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。

4

4 回答 4

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
于 2013-02-26T17:23:00.827 回答
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

提示:为避免列命名问题,如果您使用别名作为表名,请在查询中使用的所有列上使用别名。

于 2013-02-26T17:23:44.957 回答
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";
于 2013-02-26T17:24:06.797 回答
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
于 2013-02-26T17:26:12.160 回答