关于一个sql 语句,小弟我实在写不出来了,求大神帮忙
关于一个sql 语句,我实在写不出来了,求大神帮忙

执行以下语句
select g_id from spec where s_cname='深度' and r_id='1000'
返回结果为
1002
1003
1004
执行以下语句
select g_id from spec where s_cname='巴西' and r_id='1004'
返回结果为
1003
1002
执行以下语句
select g_id from spec where (s_cname='深度' and r_id='1000') and (s_cname='巴西' and r_id='1004')
应该返回结果为
1002
1003
但是为什么为空? 哪位大神能帮我把这句话写出来了 ,我要实现的 功能就是 查询咖啡口味为深度并且产地为巴西的所有咖啡..
------解决思路----------------------
------解决思路----------------------
你参考一下
------解决思路----------------------
执行以下语句
select g_id from spec where s_cname='深度' and r_id='1000'
返回结果为
1002
1003
1004
执行以下语句
select g_id from spec where s_cname='巴西' and r_id='1004'
返回结果为
1003
1002
执行以下语句
select g_id from spec where (s_cname='深度' and r_id='1000') and (s_cname='巴西' and r_id='1004')
应该返回结果为
1002
1003
但是为什么为空? 哪位大神能帮我把这句话写出来了 ,我要实现的 功能就是 查询咖啡口味为深度并且产地为巴西的所有咖啡..
------解决思路----------------------
select a.g_id
from (select g_id from spec where s_cname='深度' and r_id='1000') a
join (select g_id from spec where s_cname='巴西' and r_id='1004') b
on a.g_id = b.g_id
------解决思路----------------------
你参考一下
SELECT A.g_id FROM spec A
WHERE EXISTS(SELECT 1 FROM spec B WHERE A.g_id=B.g_id B.s_cname='深度' and B.r_id='1000')
AND EXISTS(SELECT 1 FROM spec C WHERE A.g_id=C.g_id C.s_cname='巴西' and C.r_id='1004')
GROUP BY A.g_id
------解决思路----------------------
select g_id from spec t where t.s_cname='深度' and t.r_id='1000' and
exists(select 1 from spec where g_id=t.g_id and s_cname='巴西' and r_id='1004')