嗨,我想問你的幫助,我得到了一個問題。我有一個酒店的數據庫,我想找到最長的住宿客戶。SQL最長留
我有3個表:
- 負荷消費(code_costumer,名字,姓氏),
- 住宿(code_stay,DATE_START,DATE_END)
- costumer_stay(code_stay,code_costumer )
我創建的腳本:
SELECT datediff(date_end,date_start) as dd,
stay.code_stay,costumer_stay.code_costumer
FROM stay INNER JOIN costumer_stay
ON stay.code_stay=costumer_stay.code_stay;
,但我不能讓一個人與停留時間最長,我得到類似的東西:
id|code_stay|code_costumer|
---------------------------
25|xa21 |1001 |
8 |xb24 |1005 |
7 |xb30 |1003 |
爲了找到前1的結果,應避免使用'order by'。這樣排序整個表,然後給出一個結果。 – twentylemon
非常感謝你完成了這份工作 – Kostasfra
@twentylemon:如果不通過所有的值,你還認爲DB如何確定表的最大價值? –