我有下表:
userid title content
1 gender male
1 location NY
2 gender female
2 location SF
3 gender female
3 location NY
我正在尝试仅检索具有 gender="male"和 location="NY" 的用户 ID
如果我尝试:
select userid
from table
where content="male"
AND location="NY";
它将返回空值。 知道怎么做吗?
最佳答案
SELECT t.userId
FROM yourTable AS t
INNER JOIN yourTable AS t2 ON t.userId = t2.userId
AND t.title = 'gender'
AND t.content = 'male'
AND t2.title = 'location'
AND t2.content = 'NY'
我还会考虑规范化您的数据。这将使将来的查询变得更容易(并且可能更快)。
关于mysql - sql请求-多值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8320537/