使用别名时重复的字段

时间:2018-06-08 15:09:50

标签: mysql sql

我写了一个提取可用竞赛的查询,我的表有一个名为name的公共字段,因此我使用alias在最终结果中识别此字段:

$query = "SELECT c.*,
c.name AS competition_name,
s.name AS season_name
FROM competition c
LEFT JOIN competition_seasons s ON c.id = s.competition_id
WHERE country_id = :country_id";

问题是查询将返回字段name

 {
    "id": "1093",
    "country_id": "1",
    "name": "Premier League",
    "category": "1",
    "competition_name": "Premier League",
    "season_name": "2018"
}

这是competition_name的重复,我做错了什么?

1 个答案:

答案 0 :(得分:2)

SELECT c.*,其中包含c.name

您的查询扩展为:

SELECT 
     c.id, c.country_id, c.name, c.category, 
     c.name AS competition_name, s.name AS season_name 
FROM ...

所以SELECT字段name两次。