这是表格结构:
ID PID KEY VALUE
1 2 CITY NEW YORK
2 2 COUNTRY UNITED STATES
3 2 STATE NEW YORK
4 1 CITY NEW JERSEY
等等.. 我想要的是获取/过滤结果,如
Where (KEY = CITY and VALUE = NEW YORK)
AND (key = country AND VALUE= UNITED STATES)
那是空行!
答案 0 :(得分:1)
你需要一个自我JOIN
。
SELECT t1.*
FROM my_table t1
INNER JOIN my_table t2 ON t1.pid = t2.pid
WHERE t1.key = 'city' and t1.value = 'new york'
AND t2.key = 'country' and t2.value = 'united states';
答案 1 :(得分:0)
因此,您在同一列中拥有一个包含不同类型值的表,其中一个字段标识每个集合(PID)。这是对的吗?
如果是这样,那么你需要在这里进行自我加入:
SELECT
DISTINCT cityTable.PID
FROM
tableName AS cityTable
JOIN
tableName AS countryTable
ON cityTable.PID = countryTable.PID
WHERE
cityTable.key = "city"
AND
cityTable.value = "NEW YORK"
AND
countryTable.key = "country"
AND
countryTable.value = "UNITED STATES";