我有一张这样的 table
itemid | propertyname | propertyvalue
___________|______________|_______________
1 | point | 12
1 | age | 10
2 | point | 15
2 | age | 11
3 | point | 9
3 | age | 10
4 | point | 13
4 | age | 11
我需要一个查询来选择年龄大于 10 且点小于 12 的所有项目。
我试过了
`select itemid from table where (propertyname="point" and propertyvalue < 12)
and (propertyname="age" and propertyvalue >10)`
没有结果。我怎样才能让它发挥作用?
最佳答案
您可以使用内部联接
SELECT
a.itemid
FROM
yourTable a
INNER JOIN
yourTable b
ON
a.itemid=b.itemid
AND a.propertyname='point'
AND b.propertyname='age'
WHERE
a.propertyvalue<12
AND b.propertyvalue>10
好的,所以在表 a 中查找名称为 point 且值小于 12 的所有项目,在表 b 中查找名称为 age 且值大于 10 的所有项目。然后您只需要查找项目,在两个表中。为此,您通过 itemid 连接两个表。要连接表,您可以使用连接。希望这会帮助你理解。如果没有再问:)
关于MySQL 在同一表的相同列上选择具有 AND 条件的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39488515/