我必须从两个sql语句的结果中选择常见的颜色c1,c2,c3。
1)
select c1, c2, c3,count(c3) from (select * from form_name
where data_created >'1273446000' and data_creazione<'1274569200')
group by c1,c2, c3 having count(c3)>1
2)
select c1, c2, c3,count(c3) from (select * from form_name
where data_created>'1272236400' and data_creazione<'1274569200')
group by c1,c2, c3 having count(c3)>2
我需要选择c1,c2,c3在查询结果中同样和常见。
怎么可能这样做......有人可以帮忙吗?
答案 0 :(得分:5)
从选择列表中删除count(c3)
,它可以不同(HAVING子句保证这一点),OP只想比较c1,c2和c3。如果COUNT(c3)
列不同,哪些行可以共同使用?没有或一些,它会有所不同。同时删除派生表,它们不是必需的。所以试试:
select
c1, c2, c3
from form_name
where data_created >'1273446000' and data_creazione<'1274569200'
group by c1,c2, c3
having count(c3)>1
INTERSECT
select
c1, c2, c3
from form_name
where data_created>'1272236400' and data_creazione<'1274569200'
group by c1,c2, c3
having count(c3)>2
答案 1 :(得分:2)
您是否尝试使用'UNION'加入2个查询?
例如
select c1, c2, c3,count(c3) from (select * from form_name
where data_created >'1273446000' and data_creazione<'1274569200')
group by c1,c2, c3 having count(c3)>1
union
select c1, c2, c3,count(c3) from (select * from form_name
where data_created>'1272236400' and data_creazione<'1274569200')
group by c1,c2, c3 having count(c3)>2
答案 2 :(得分:2)
我认为INTERSECT
会解决您的问题。更多信息here。
select c1, c2, c3,count(c3) from (select * from form_name
where data_created >'1273446000' and data_creazione<'1274569200')
group by c1,c2, c3 having count(c3)>1
INTERSECT
select c1, c2, c3,count(c3) from (select * from form_name
where data_created>'1272236400' and data_creazione<'1274569200')
group by c1,c2, c3 having count(c3)>2
答案 3 :(得分:0)
您可以使用派生表,然后将它们连接在一起以获得所需的结果。
select a.c1, a.c2, a.c3, a.acount, b.bcount
From
(select c1, c2, c3, count(*) as acount from (select * from form_name
where data_created >'1273446000' and data_creazione<'1274569200')
group by c1,c2, c3 having count(c3)>1) a
join
(select c1, c2, c3, count(*) as bcount from (select * from form_name
where data_created>'1272236400' and data_creazione<'1274569200')
group by c1,c2, c3 having count(c3)>2)b
on a.c1 = b.c1 and a.c2 = b.c2 and a.c3 = b.c3