這條sql語句有什麼問題?請幫忙!(30分)

  • 主题发起人 主题发起人 杜風
  • 开始时间 开始时间

杜風

Unregistered / Unconfirmed
GUEST, unregistred user!
SELECT *
FROM LEVEL_QUERY
WHERE (adjust_level_enable_date IN
(SELECT MAX(adjust_level_enable_date)
AS adjust_level_enable_date
FROM level_query
GROUP BY id))

本來相找出level_query中所有id 相同的記錄中adjust_level_enable_date(日期)最大的一條,
但有的記錄得出了多條id相同的記錄?why?
 
你没有体现id相同嘛
 
select id, max(adjust_level_enable_date) as adjust_level_enable_date from level_query
group by id
 
這樣寫是可以(沒錯),但問題是我要得出該表中所有字段?
 
SELECT MainQuery.*
FROM LEVEL_QUERY MainQuery
WHERE (adjust_level_enable_date IN
(SELECT MAX(adjust_level_enable_date)
AS adjust_level_enable_date
FROM level_query SubQuery
GROUP BY SubQuery.id
HAVING SubQuery.id = MainQuery.id))
 
Select AAA.*
From LEVEL_QUERY AAA,
(select id,max(adjust_level_enable_date) as rq
from LEVEL_QUERY
group by id) BBB
Where AAA.id=BBB.id and AAA.adjust_level_enable_date=BBB.rq
 
select b.*
from
(select id, max(adjust_level_enable_date) as adjust_level_enable_date from level_query
group by id) a
right outer join level_query b on b.id=a.id
where b.adjust_level_enable_date=a.adjust_level_enable_date
 
多人接受答案了。
 
SELECT MainQuery.*
FROM LEVEL_QUERY MainQuery
WHERE (adjust_level_enable_date IN
(SELECT MAX(adjust_level_enable_date)
AS adjust_level_enable_date
FROM level_query SubQuery
GROUP BY SubQuery.id
HAVING SubQuery.id = MainQuery.id))
 
后退
顶部