MySQL select one field from table WHERE condition is in multiple rows(MySQL 从表中选择一个字段 WHERE 条件在多行中)
问题描述
试图找到答案,但还是找不到..表格如下:
<上一页>id、关键字、值1 显示 15.61个硬盘3201 公羊 3所以我需要的是这样的.. 从这个表中选择一个 ID,其中 (keyword="display" and value="15.6") AND (keyword="harddrive" and value="320")代码>也有可能会有 3 或 4 个这样的关键字条件导致返回一个 id(一行)
似乎有一些东西要处理UNION,但我之前没有使用它,所以我无法弄清楚
提前致谢
这是一个关系除法问题.像下面这样的东西应该可以做到.
选择 idFROM your_table在哪里(keyword="display" and value="15.6") OR (keyword="harddrive" and value="320")按 ID 分组有计数(*)= 2
我假设您的表具有适当的约束,因此不可能有完全重复的行.(例如id,keyword
上有PK)
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)
It seems there's something to deal with UNION but i didn't use it before so i can't figure it out
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. (e.g. there is a PK on id, keyword
)
这篇关于MySQL 从表中选择一个字段 WHERE 条件在多行中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL 从表中选择一个字段 WHERE 条件在多行中


基础教程推荐
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01