考虑这个问题:
select count(*) from
(
select pickle_id,
pickle_code
from pickles
where pickle_code='DILL'
group by pickle_id,
pickle_code /* Returns 1000 rows */
minus
select pickle_id,
pickle_code
from relish inner join pickles on relish_id=pickle_id
where relish_code ='BURGER' /* Returns 1500 rows */
)
/* Complete query returns 900 rows */
问题:
如何编写返回原始1000行的查询 只有从减号查询中减去的100个?
结果应该总共1100行(1000 + 100减去)
答案 0 :(得分:2)
试试这个
select pickle_id,
pickle_code
from pickles
where pickle_code='DILL'
group by pickle_id,
pickle_code /* Returns 1000 rows */
union all
(
select pickle_id,
pickle_code
from pickles
where pickle_code='DILL'
group by pickle_id,
pickle_code /* Returns 1000 rows */
intersect
select pickle_id,
pickle_code
from relish inner join pickles on relish_id=pickle_id
where relish_code ='BURGER' /* Returns 1500 rows */
)
答案 1 :(得分:1)
嗯,那就是minus
之前的所有内容:
select pickle_id, pickle_code
from pickles
where pickle_code = 'DILL'
group by pickle_id, pickle_code;
编辑:
评论中问题的答案是:
select count(*)
from ((select pickle_id, pickle_code
from pickles
where pickle_code='DILL'
group by pickle_id, pickle_code /* Returns 1000 rows */
) intersect
(select pickle_id, pickle_code
from relish inner join pickles on relish_id=pickle_id
where relish_code ='BURGER' /* Returns 1500 rows */
)
) p;
答案 2 :(得分:0)
select count(*) from (
select pickle_id,
pickle_code
from pickles
where pickle_code='DILL'
group by pickle_id,
pickle_code
union all
select pickle_id,
pickle_code
from relish inner join pickles on relish_id=pickle_id
where relish_code ='BURGER' and pickle_code='DILL'
group by pickle_id, pickle_code
)