Charanoglu
Charanoglu

Reputation: 1339

Duplicated field when using alias

I wrote a query that extract the competitions available, and my table have a common field called name so I used the alias for recognize this field in the final result:

$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";

the problem's that the query will return the field name:

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

that is a duplication of competition_name, what I did wrong?

Upvotes: 0

Views: 19

Answers (1)

Bart Friederichs
Bart Friederichs

Reputation: 33511

You SELECT c.*, which includes c.name:

Your query expands to:

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

so you SELECT the name field twice.

Upvotes: 2

Related Questions