sql1:
SELECT a.* FROM td_send a,
(SELECT ID FROM td_send GROUP BY ID HAVING MAX(STEPNOW)=2) b
WHERE a.ID=b.id;
sql2:
SELECT a.* FROM td_send a where STEPNOW=2;
以上两个SQL有什么不同吗?
------解决方案--------------------
第一个是以ID为筛选列,第二个是以STEPNOW为筛选列,能一样么
WITH t AS
(select 1 ID ,0 step from dual UNION
select 1 ID ,1 step from dual UNION
select 1 ID ,2 step from dual UNION
select 2 ID ,0 step from dual UNION
select 2 ID ,1 step from dual )
/*SELECT ID FROM t GROUP BY ID HAVING MAX(STEP)=2*/
/*SELECT a.* FROM t a where step=2;*/
SELECT a.* FROM t a,
(SELECT ID FROM t GROUP BY ID HAVING MAX(STEP)=2) b
WHERE a.ID=b.id;
------解决方案--------------------
一样与否关键在于ID是不是唯一的(或主键)
如果ID是主键,那么结果完全一致
如果ID不是主键,结果会有不同
前一个sql的意思是:查询出同一ID中相应的STEPNOW最大值为2的所有记录
后一个sql的意思是:查出STEPNOW=2的所有记录