Mysql join返回多行但是php foreach需要单个记录[重复]

This question already has an answer here:

I have a simple join query :

select s.id, s.account_id, s.startdate, ss.skill_id, sk.skill_name 
from shyfts s 
   join shyfts_skills ss on ss.shyft_id = s.id 
   join skills sk on sk.id=ss.skill_id

This give me the results I expected

id      account_id  startdate   skill_id    skill_name
19783   35          2018-10-10  68          Delivery
19783   35          2018-10-10  67          Cooking
28766   76          2018-10-18  68          Delivery

The problem I have is I need to do a loop (foreach in php) but I only need to show unique shift so by ID. As you can see the shift id 19783 is a duplicate. Of course, I can do a GROUP BY but there will be one skill missing (Cooking).

I can do it by adding another select within the foreach-loop but it take too much performance on 200+ rows.

Or, maybe something like "if current $row['id'] is different than the previous $row['id'] but this is bad coding I think.

How can I do that ??

</div>

Try to check GROUP_CONCAT(fieldName SEPARATOR ',') this function, you may concat just one field, and ofc. use GROUP BY. That could be like mysql join with multiple values in one column this one question

I guess that you can use select distinct statement for not show duplicates..

select distinct s.id, s.account_id, s.startdate, ss.skill_id, sk.skill_name 
from shyfts s 
   join shyfts_skills ss on ss.shyft_id = s.id 
   join skills sk on sk.id=ss.skill_id