given the two tables and values:
Tables
People- Columns: [ID]PK Name
Field - Columns: [ID FieldName]PK FieldValue
Values
People
ID Name
1 Mary
2 John
3 Tony
Field
ID FieldName FieldValue
1 Age 20
1 Country USA
2 Age 21
2 Country USA
3 Age 20
3 Country USA
I would like a query that gives me the names of the people from USA AND have 20 years old. The result should be Mary and Tony.
SELECT name FROM people p
INNER JOIN field f ON f.ID = f.ID
WHERE
(FieldName = 'Age' AND FieldValue= '20') OR
(FieldName = 'Country' AND FieldValue= 'USA'));
The problem with that query is the “OR” between the where clauses. I will get the 3 people, instead of only Mary and Tony. If I use an AND, none result is return.
Do you have some tips?
Thanks in advance 🙂
What about this?
Thereby treating the Country and Age data essentially as separate tables. As it seems that the
fieldtable is used to store different types of data.It might be useful to have different database
Viewson this table for each type of data. This will further simplify the above query.