您好我有以下数据表
A B bid status
10 20 1 SUCCESS_1
10 20 1 SUCCESS_2
10 30 2 SUCCESS_1
10 30 2 SUCCESS_2
现在我想根据SUCCESS_1和SUCCESS_2打印或计算上面的行数。我创建了以下查询,但它不起作用,只是通过组合两行返回一行。
select * from tbl t1 join tbl t2 on
on (t1.A=t2.A and t1.B=t2.B and
(t1.Status = 'SUCCESS_1' and t2.Status = 'SUCCESS_2')
where t1.bid= 1
我希望输出如上面的查询
A B bid status
10 20 1 SUCCESS_1
10 20 1 SUCCESS_2
我是SQL新手请指导。提前谢谢。
答案 0 :(得分:0)
SELECT * FROM `tbl1` WHERE `bid`=1 GROUP BY `status`
答案 1 :(得分:0)
如果由于某种原因需要进行连接(例如,如果按1列分组,您的数据库不允许您选择所有内容,因为它希望将所有内容都分组或成为聚合),您可以执行以下操作:
select t1.*
from tbl t1 join tbl t2
on (t1.A=t2.A and t1.B=t2.B and t1.Status = 'SUCCESS_1' and t2.Status = 'SUCCESS_2')
where t1.bid= 1
union all select t2.*
from tbl t1 join tbl t2
on (t1.A=t2.A and t1.B=t2.B and t1.Status = 'SUCCESS_1' and t2.Status = 'SUCCESS_2')
where t1.bid= 1
order by 1,2,3,4
您的原始查询正在拉回一行中的所有数据,但是这一行会拉回分别生成连接行的两行。