数据库查询数据问题

2019-06-18 17:04:33 +08:00
 a476286557
id  pid  type
1    1    2
2    1    7
3    1    8
4    2    8
5    3    7
6    3    8

表结构如上。
pid 与 id 没有任何关系
取出来这两种数据:
1。pid 为 2 的,因为 type 只等于 8
2。pid 为 3 的,因为 type 只有等与 7 与等于 8 的
注:pid 为 1 的不需要,因为有一个 type=2,所以不取。

1735 次点击
所在节点    Python
12 条回复
VeryZero
2019-06-18 17:49:27 +08:00
完全不知道在说啥。。
iblislsy
2019-06-18 18:30:50 +08:00
完全不知道在说啥。。
AngryPanda
2019-06-18 18:32:32 +08:00
语文不及格。
KannaMakino
2019-06-18 18:40:34 +08:00
???
ashlord
2019-06-18 18:42:22 +08:00
额所以问题呢?
sharpless
2019-06-18 18:48:52 +08:00
表达的很不准确
akira
2019-06-18 18:57:26 +08:00
1。pid 为 2 的,因为 type 只等于 8
// pid=2 和 type=8 有什么逻辑关系?

2。pid 为 3 的,因为 type 只有等与 7 与等于 8 的
// pid=3 和 type=7/8 又是什么逻辑关系

注:pid 为 1 的不需要,因为有一个 type=2,所以不取
// pid=1 和 type =2 又又是什么逻辑关系
lyy16384
2019-06-18 19:18:00 +08:00
说实话你的附言依然看不懂
1010543618
2019-06-18 19:36:57 +08:00
感觉可以这样 xxx 代表一些 procedure

select pid from (select xxx(type) as type_str, pid group by pid) where xxx(type_str)
1010543618
2019-06-18 19:38:52 +08:00
貌似是要根据某个 pid 是否只包含某个 type,找出符合的 pid
RRRoger
2019-06-18 19:51:17 +08:00
SELECT * FROM table1 a
WHERE NOT EXISTS
(SELECT 1 FROM table1 b
WHERE a.pid=b.pid
AND a.type <b.type)
maierhuang
2019-06-18 19:56:41 +08:00
select * from t1 where pid in (select distinct pid from t1 where type in (7,8) and pid not in (select distinct pid from t1 where type !=7 and type !=8));

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

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

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

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

© 2021 V2EX