2013-02-26 72 views
0
SELECT COUNT(a.aircraft) as total 
    , a.aircraft 
    , b.fullname AS aircraft_name 
FROM db_pireps AS a 
JOIN db_aircraft AS b ON a.aircraft = b.id 
WHERE pilotid = {$pilotid} 
GROUP BY aircraft 
ORDER BY total DESC 
LIMIT 6 

我有這個查詢,但我想增加b.registration AS reg,但我的嘗試似乎失敗了,因爲我不知道如何將另一個SELECT放在該查詢中。添加另一個選擇

+1

你正在得到什麼錯誤? – DevelopmentIsMyPassion 2013-02-26 17:22:12

+1

您可以顯示出現錯誤的查詢嗎? – Barmar 2013-02-26 17:22:16

回答

1

使用逗號:

SELECT 
    COUNT(a.aircraft) as total, 
    a.aircraft, 
    b.fullname AS aircraft_name, 
    b.registration AS reg 
FROM db_pireps AS a JOIN db_aircraft AS b 
    ON a.aircraft = b.id WHERE pilotid = {$pilotid} 
GROUP BY aircraft ORDER BY total DESC LIMIT 6 
+0

你每天都會學到新東西,謝謝。我從來不知道你可以用逗號。 – zzwyb89 2013-02-26 17:25:20

+1

@ zzwyb89:真的嗎?你沒看過其餘的查詢嗎?你沒有寫這個查詢開始? – 2013-02-26 17:25:47

1
SELECT COUNT(a.aircraft) as total, a.aircraft, b.fullname AS aircraft_name, b.registration AS reg 
FROM db_pireps AS a JOIN db_aircraft AS b ON a.aircraft = b.id 
WHERE a.pilotid = {$pilotid} 
GROUP BY a.aircraft 
ORDER BY total DESC LIMIT 6 

提示:爲了避免問題的列命名,如果使用別名表名,使用別名您正在使用查詢中的所有列。

1

我真的不明白你的問題,但我認爲這應該工作:

$aircraft_query = "SELECT COUNT(a.aircraft) as total, a.aircraft, b.fullname AS aircraft_name, b.registration AS reg 
        FROM db_pireps AS a JOIN db_aircraft AS b ON a.aircraft = b.id 
        WHERE pilotid = {$pilotid} 
        GROUP BY aircraft 
        ORDER BY total DESC LIMIT 6"; 
0

你只需要添加列b.registration並給它別名爲REG。見下文。它與你所做的相同

SELECT COUNT(a.aircraft) as total, a.aircraft, b.fullname AS aircraft_name, b.registration AS reg 
FROM db_pireps AS a JOIN db_aircraft AS b ON a.aircraft = b.id 
WHERE pilotid = {$pilotid} 
GROUP BY a.aircraft 
ORDER BY total DESC LIMIT 6