Hello. Please bear with me. This is a little complex and I'm pulling my hair out. I hope you can help (not pull my hair out, but with this problem.
I have 4 tables that look like this:
CATEGORIES
ID Category
1 Contacts
2 Credit Cards
3 Work Related
...
SUBCATEGORIES
ID SubCategory CategoryID
1 Mark 1
2 Ben 1
3 VISA 2
4 Capital One 2
5 Dawn 1
FIELDS
ID FieldName CategoryID
1 Phone 1
2 Email 1
3 Number 2
4 Expiration Date 2
5 Mobile Phone 1
FIELDVALUES
ID FIELDID SUBCATEGORYID VALUE
1 1 2 502-555-BEN1
2 1 1 502-555-MARK
3 3 3 5044123412341244
4 5 5 502-555-DAWN
5 2 1 mark@bellsouth.net
All I really need is to do a left join on fields and fieldvalues, getting all field columns FOR A PARTICULAR SUBCATEGORY ID (which is supplied) and all MATCHING fieldnames from fields.
The select below isn't right--it gets all fields in a particular category, but I need one that gets all fields AND field values for a particular subcategory.
SELECT fields.fieldname
from categories left join fields on fields.categoryid = categories.id
where categories.id = 3
In plain terms, if I supply a SubCategory ID of 1 (for Mark), I want to see all fields that map to him. In the case above, that will be:
Phone 502-555-1212
Email mark@bellsouth.net
I've been out of SQL for years, and honestly can't seem to make this work. Can anyone help me with this query?
Thanks for any info.
-Brian
I have 4 tables that look like this:
CATEGORIES
ID Category
1 Contacts
2 Credit Cards
3 Work Related
...
SUBCATEGORIES
ID SubCategory CategoryID
1 Mark 1
2 Ben 1
3 VISA 2
4 Capital One 2
5 Dawn 1
FIELDS
ID FieldName CategoryID
1 Phone 1
2 Email 1
3 Number 2
4 Expiration Date 2
5 Mobile Phone 1
FIELDVALUES
ID FIELDID SUBCATEGORYID VALUE
1 1 2 502-555-BEN1
2 1 1 502-555-MARK
3 3 3 5044123412341244
4 5 5 502-555-DAWN
5 2 1 mark@bellsouth.net
All I really need is to do a left join on fields and fieldvalues, getting all field columns FOR A PARTICULAR SUBCATEGORY ID (which is supplied) and all MATCHING fieldnames from fields.
The select below isn't right--it gets all fields in a particular category, but I need one that gets all fields AND field values for a particular subcategory.
SELECT fields.fieldname
from categories left join fields on fields.categoryid = categories.id
where categories.id = 3
In plain terms, if I supply a SubCategory ID of 1 (for Mark), I want to see all fields that map to him. In the case above, that will be:
Phone 502-555-1212
Email mark@bellsouth.net
I've been out of SQL for years, and honestly can't seem to make this work. Can anyone help me with this query?
Thanks for any info.
-Brian