一个单表查询的问题

student_2008 2018-12-28 09:10:41

CREATE TABLE `product_config` (
`product_id` bigint(20) NOT NULL COMMENT '产品ID',
`config_id` bigint(20) NOT NULL COMMENT '产品配置属性ID'
)

INSERT INTO `product_config`(`product_id`, `config_id`) VALUES (4, 7);
INSERT INTO `product_config`(`product_id`, `config_id`) VALUES (6, 2);
INSERT INTO `product_config`(`product_id`, `config_id`) VALUES (6, 7);
INSERT INTO `product_config`(`product_id`, `config_id`) VALUES (6, 14);

想查询出config_id等于2,7,14的product_id
...全文
1073 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
Inter_Ares 2019-01-03
  • 打赏
  • 举报
回复
SELECT t1.product_id FROM
(SELECT product_id,config_id from t_test WHERE config_id=2) t1,
(SELECT product_id,config_id from t_test WHERE config_id=7) t2,
(SELECT product_id,config_id from t_test WHERE config_id=14) t3
where t1.product_id = t2.product_id AND t2.product_id = t3.product_id
Flyinsky1 2018-12-30
  • 打赏
  • 举报
回复
select count(product_id) as pc ,product_id from product_config where config_id in (2,7,14) group by product_id having pc>2;
octwind 2018-12-29
  • 打赏
  • 举报
回复
select distinct a.product_id from product_config a ,product_config b , product_config c where
a.product_id = b.product_id and b.product_id = c.product_id and
a.config_id = 2 and b.config_id = 7 and c.config_id = 14
∈best 2018-12-29
  • 打赏
  • 举报
回复
elect distinct product_id from product_config where config_id=2 or config_id=7 or config_id=14
Groovy Java 2018-12-29
  • 打赏
  • 举报
回复
select product_id from product_config where config_id in (2,7,14)
student_2008 2018-12-29
  • 打赏
  • 举报
回复
引用 16 楼 octwind 的回复:
select distinct a.product_id from product_config a ,product_config b , product_config c where
a.product_id = b.product_id and b.product_id = c.product_id and
a.config_id = 2 and b.config_id = 7 and c.config_id = 14

那如果多个config_id时候,要自关联多次么?
楓VS痕 2018-12-28
  • 打赏
  • 举报
回复
select distinct(product_id) pi from product_config where config_id in(2,7,14) order by pi desc limit 1;
student_2008 2018-12-28
  • 打赏
  • 举报
回复
引用 4 楼 feng2 的回复:
[quote=引用 3 楼 student_2008 的回复:]
[quote=引用 2 楼 feng2 的回复:]
select distinct product_id from product_config where config_id in(2,7,14);

这样查出来是4和6,只想要看到6的记录[/quote]

你这个想法和你的需求不一致。
把需求描述清楚吧。[/quote]
看清楚需求在回
feng2 2018-12-28
  • 打赏
  • 举报
回复
引用 3 楼 student_2008 的回复:
[quote=引用 2 楼 feng2 的回复:]
select distinct product_id from product_config where config_id in(2,7,14);

这样查出来是4和6,只想要看到6的记录[/quote]

你这个想法和你的需求不一致。
把需求描述清楚吧。
student_2008 2018-12-28
  • 打赏
  • 举报
回复
引用 2 楼 feng2 的回复:
select distinct product_id from product_config where config_id in(2,7,14);

这样查出来是4和6,只想要看到6的记录
feng2 2018-12-28
  • 打赏
  • 举报
回复
select distinct product_id from product_config where config_id in(2,7,14);
  • 打赏
  • 举报
回复
SELECT * FROM ( SELECT a.product_id AS a1, group_concat( a.config_id ORDER BY config_id ASC ) AS a2 FROM product_config a GROUP BY a.product_id ) b WHERE a2 = '2,7,14' 大概这样。用group_concat函数 把config_id 合并。 然后只要合并后等于 2,7,14的
luyaran 2018-12-28
  • 打赏
  • 举报
回复
求出来数据及之后再次重新处理不就行了,用代码具体判断
吉普赛的歌 2018-12-28
  • 打赏
  • 举报
回复
我 #9 的已经可以了呀
student_2008 2018-12-28
  • 打赏
  • 举报
回复
引用 10 楼 楓VS痕 的回复:
[quote=引用 8 楼 student_2008 的回复:]
[quote=引用 6 楼 楓VS痕 的回复:]
select distinct(product_id) pi from product_config where config_id in(2,7,14) order by pi desc limit 1;

像这样,就希望得到1和2[/quote]
关于 product_id,有1,2,3,4,你想得到1和2;查出来是4和6,只想要看到6的记录,你是根据什么判断只想看到6,或者1和2?
[/quote]
按照这个图的结果,就是希望看到config_id等于,2,7,14的记录product_id,那就是只有product_id是1和2的2个满足条件
alex259 2018-12-28
  • 打赏
  • 举报
回复
他应该是要查询出,同时包含2,7,14的product_id

引用 10 楼 楓VS痕 的回复:
[quote=引用 8 楼 student_2008 的回复:]
[quote=引用 6 楼 楓VS痕 的回复:]
select distinct(product_id) pi from product_config where config_id in(2,7,14) order by pi desc limit 1;

像这样,就希望得到1和2[/quote]
关于 product_id,有1,2,3,4,你想得到1和2;查出来是4和6,只想要看到6的记录,你是根据什么判断只想看到6,或者1和2?
[/quote]
楓VS痕 2018-12-28
  • 打赏
  • 举报
回复
引用 8 楼 student_2008 的回复:
[quote=引用 6 楼 楓VS痕 的回复:]
select distinct(product_id) pi from product_config where config_id in(2,7,14) order by pi desc limit 1;

像这样,就希望得到1和2[/quote]
关于 product_id,有1,2,3,4,你想得到1和2;查出来是4和6,只想要看到6的记录,你是根据什么判断只想看到6,或者1和2?
吉普赛的歌 2018-12-28
  • 打赏
  • 举报
回复
SELECT  
distinct product_id
from product_config as a
where config_id in (2,7,14)
and exists(
    select 1 from product_config as b where a.product_id=b.product_id and b.config_id=7
)
and exists(
    select 1 from product_config as c where a.product_id=c.product_id and c.config_id=14
);

student_2008 2018-12-28
  • 打赏
  • 举报
回复
引用 6 楼 楓VS痕 的回复:
select distinct(product_id) pi from product_config where config_id in(2,7,14) order by pi desc limit 1;

像这样,就希望得到1和2
student_2008 2018-12-28
  • 打赏
  • 举报
回复
引用 6 楼 楓VS痕 的回复:
select distinct(product_id) pi from product_config where config_id in(2,7,14) order by pi desc limit 1;

这样只有一个product_id了,如果表product_config有多个product_id条件都满足2,7,14就查不到了

56,687

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧