mysql 联合主键 做查询

2018-02-13 17:04:47 +08:00
 longnight0119

mysql 中表名 table 复合主键 id,type 通过其他系统 获得一堆 主键 [{1,1},{2,1},{3,1},{4,2},{5,2},{6,2}……] 请问 我在 table 中 如何通过最简单的 sql 语句查询出 我所拥有的 复合主键数组 相关的实体 select * from table where condationSql 这个 condationSql 我该怎么写

9170 次点击
所在节点    MySQL
9 条回复
Ghkitg
2018-02-13 17:10:01 +08:00
SELECT *
FROM t
WHERE (col1, col2)
IN ((val1a, val2a), (val1b, val2b), ...) ;
longnight0119
2018-02-13 17:10:47 +08:00
好的 我试一下,稍后在回复,
longnight0119
2018-02-13 17:13:50 +08:00
@Ghkitg 解决了 这个写法真的是 不好搜 ,百度了 什么结果都没有 ,还真的是好用 ,谢谢, 请问相关的知识 我在哪里能补一下 ,还请不吝赐教
Ghkitg
2018-02-13 17:15:00 +08:00
longnight0119
2018-02-13 17:55:14 +08:00
@Ghkitg 假如说我要 group by (id,type) having count(id,type) =2 要实现这样的需求 该怎么 写 我试了
group by (id,type) having count(id,type) =2 这样的方式 查询 error:Operand should contain 1 column(s)
wanganjun
2018-02-13 18:02:17 +08:00
@longnight0119 直接 having count() = 2 应该就可以了
longnight0119
2018-02-13 18:04:11 +08:00
@wanganjun 不行 我试了 error:[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')=2
longnight0119
2018-02-13 18:06:56 +08:00
@wanganjun 假如说我要 group by (id,type) having count(id,type) =2 要实现这样的需求 该怎么 写 我试了
group by (id,type) having count(id,type) =2 这样的方式 查询 error:Operand should contain 1 column(s)
这个是因为 group by 后面不能跟 圆括号,把圆括号 去掉以后 执行查询 就被阻挡在 having count 这里了
longnight0119
2018-02-13 18:07:40 +08:00
@wanganjun 然后 把 having count() 中的内容去掉 就是[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')=2

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/430599

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX