添加另一个选择

时间:2013-02-26 17:19:15

标签: php mysql

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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

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

答案 2 :(得分: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";

答案 3 :(得分: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