Help with mysql query or database design to help me achieve this
I have been a long time reader of site but havenever posted before and am hoping someone here can help. I am working on a multi-user web application which allows users to capture prospect information into our database. The users will be given default fields inside the app, such as name and email which are grouped into data groups.
However i want to allow the users to create as many custom fields as they wish and also allow them to choose which data group these custom fields belong in group. I am struggling to figure out the best way to achieve this.
I have currently have 3 tables in my data which are as follows:
datagroups
group_id group_name order
1 test group 1 2
2 test group 2 1
datafields
field_id field_name group
1 field 1 2
2开发者_如何转开发 field 2 1
3 field 3 2
customdatafields
custom_field_id field_name group
1 custom 1 1
2 custom 2 2
I am really puzzled on how I would create a query across the 3 tables so that I can produce a view following display:
test group 2
- field 1
- field 3
- custom 2
test group 1
- field 2
- custom 1
One thing I need to keep in mind is that I may allow the users to create custom data groups also that that needs to be factored into this.
Any input on this would be much appreciated.
Thanks
However i want to allow the users to create as many custom fields as they wish and also allow them to choose which data group these custom fields belong in group.
You've just made every casual user your database designer.
Does that sound like a good idea?
Anything that should be a row in a table will end up being a comma separated string in a single column. Data will end up looking like this, sooner or later.
Home: 123-456-7890, Work: 123-456-0987, Cell: 123-454-5678
H: 123-454-6453, W: 123-432-5746, 800: 1-800-555-1212
234-345-4567, 234-345-6785
323-123-4567 Don't call before 10:00 am
Why would people do that? Because ordinary people don't know how to design databases. They'll do the most expedient thing, which is just put data in any which way now, and figure out how to deal with it later.
Heck, most of the programmers on SO don't know how to design databases. Just read [database] questions for a week or so. (Including a dozen variants of this very one.)
You do not want to use MySQL or any SQL dbms for this. If you can't do a proper job of modeling to start with, a SQL dbms will just make you die before your time.
well, your described output is not tabular... so not directly what you will get form a sql statement.
however, you should be able to get listings by using UNION maybe something like this:
select g.group_id, g.group_name, f.field_name
from datagroups g,
(select field_name, field_id, group from datafields
union
select custom_field_name, field_id, group from customdatafields
) f
where f.group = g.group_id
order by g.order
精彩评论