Cyclone
Cyclone

Reputation: 15259

Query selects duplicated records

I want to select all records from website.advert and join player name from different table also. The query runs itself without any errors, but there is a problem that it does select duplicated records from website.advert when there is more than one record with the same account_id in the player table.

Here is my query:

  SELECT `advert`.*, `p`.`name`, `p`.`level`
   FROM `website`.`advert`
   LEFT JOIN `player`.`player` `p`
   ON (`p`.`account_id` = `website`.`advert`.`author_id`)
   WHERE `advert`.`accepted` = 0

Upvotes: 0

Views: 116

Answers (2)

Barry Chapman
Barry Chapman

Reputation: 6780

You need to use DISTINCT but I am having trouble figuring out your query. You don't seem to be selecting any fields from website.advert

Sorry, edited:

SELECT `advert`.*, `p`.`name`, `p`.`level`
 FROM `website`.`advert`
 LEFT JOIN `player`.`player` `p`
 ON (`p`.`account_id` = `website`.`advert`.`author_id`)
 WHERE `advert`.`accepted` = 0
 GROUP BY `advert`.`id`

We are JUST selecting the id field for now - try that and see if it gives you closer to the results you are expecting.

Upvotes: 2

Somnath Muluk
Somnath Muluk

Reputation: 57656

Try this query:

SELECT a.*, p.name, p.level
   FROM advert a LEFT JOIN player p
   ON (p.account_id = a.author_id)
   WHERE a.accepted = 0
group by p.account_id

Upvotes: 1

Related Questions