INNER or LEFT Joining Multiple Table Records Into A Single Row

前端 未结 3 1914
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-20 10:10

Phone Table

+----------------+-------------+
| Field          | Type        |
+----------------+-------------+
| f_id           | int(15)     |
|         


        
3条回答
  •  暖寄归人
    2020-12-20 10:31

    Though you can join several numbers (in any) into a single field:

    SELECT
      CONCAT(f_first_name, ' ', f_last_name) as Client_Name,
      GROUP_CONCAT(IF(phone_type='work',f_phone_number, NULL)) as Work_Numbers,
      GROUP_CONCAT(IF(phone_type='home',f_phone_number, NULL)) as Home_Numbers
    FROM clients
    JOIN phone
      USING (f_id)
    WHERE phone_type IN ('home', 'work')
    GROUP BY f_id;
    

提交回复
热议问题