简体   繁体   English

MySQL从表中选择一个字段WHERE条件是多行

[英]MySQL select one field from table WHERE condition is in multiple rows

Tried to find the answer, but still couldn't.. The table is as follows: 试图找到答案,但仍然不能..表格如下:

id, keyword,   value
1   display    15.6
1   harddrive  320
1   ram        3

So what i need is something like this.. Select an id from this table where (keyword="display" and value="15.6") AND (keyword="harddrive" and value="320") There's also a possibility that there will be 3 or 4 such keyword conditions which should result into returning one id (one row) 所以我需要的是这样的..从这个表中选择一个id (keyword="display" and value="15.6") AND (keyword="harddrive" and value="320")那里也有可能将是3或4个这样的关键字条件,这将导致返回一个id(一行)

It seems there's something to deal with UNION but i didn't use it before so i can't figure it out 似乎有一些事情要处理UNION,但我之前没有使用它,所以我无法弄明白

Thanks in advance 提前致谢

This is a relational division problem. 这是一个关系划分问题。 Something like the following should do it. 像下面这样的东西应该这样做。

SELECT id
FROM your_table
WHERE 
(keyword="display" and value="15.6") OR (keyword="harddrive" and value="320")
GROUP BY id
HAVING COUNT(*) = 2

I'm assuming that your table has appropriate constraints such that it is impossible for there to be a completely duplicated row. 我假设您的表具有适当的约束,因此不可能存在完全重复的行。 (eg there is a PK on id, keyword ) (例如, id, keyword上有PK)

SELECT DISTINCT id FROM table
WHERE 
(keyword="display" and value=15.6) OR (keyword="harddrive" and value=320)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM