mysql - sql请求-多值

标签 mysql sql

我有下表:

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/

相关文章:

mysql - 列出 "copying to tmp table on disk"MySQL 查询

sql - Entity Framework 在插入其他表时未引用的表中创建新记录

sql - MySQL 计算字段中的 IF 条件

sql - 按行号更新行

sql - 在单个 SQL Server 实例中使用多个数据库排序规则会出现哪些问题?

iphone - 如何在单个查询中插入1条记录后检索自动增量值(sql server)

php - 生产中的 Laravel 应用程序中不显示图像

MySql 插入到用户名=某些内容的特定行

PHP 未将数据插入 MySQL 但未显示错误

php - 使用php将while循环中的数据插入到表中