2014-03-29 88 views
0

有兩個表在這種情況下的SQL(SQLite)查詢?

User 
======== 
uid 
name 

Data 
========= 
distance 
timeSpend 
isShow 
uid (FK) 

我想獲得以下citeria

1) in a specific timeSpend Range 
2) group by the uid (Only select the longest Distance) 
3) only isShow 

嘗試下面的查詢,但沒有運氣的距離的順序列表(DESC)。感謝您的幫助

SELECT User.name, Data.distance, Data.timeSpend 
FROM FROM User,Data 
WHERE id IN (
    SELECT MAX(distance) FROM Data GROUP BY uid WHERE isShow = true 
    ) 
AND User.uid = Data.uid 
ORDER BY Data.distance DESC 

回答

1

你必須填寫between聲明你喜歡

SELECT u.name, MAX(distance) as max_distance 
FROM User u 
join Data d on u.uid = d.uid 
WHERE isShow = 1 
and d.timeSpend between 1 and 2 
group by u.uid, u.uname 
ORDER BY max_distance DESC 
+0

感謝您的幫助值 – user782104