开发者

grouping data and showing it

I have 2 tables

Table 1: Profiles

id
name
url

Table 2: Products

id
name
profileid (Reference Key, Profiles.id)开发者_运维技巧

Now, tables have following data

Table 1: Profiles

id     name     url
-----------------------------
1      aaa      http://a.com
2      bbb      http://b.com
3      ccc      http://c.com

Table 2:

id     name     profileid
-----------------------------
1      mmm      1
2      nnn      1
3      ooo      2

Now I need output as

profileid     profilename     productname
------------------------------------------
1              aaa               mmm
                                 nnn
2              bbb               ---
3              ccc               ---

I know that left-join will do the trick, but it repeats the profile name. Is there any way to get profile id and name single time, and list of all associated products.


SELECT P1.id AS profileid
     , P1.name AS profilename
     , GROUP_CONCAT(P2.productname)
  FROM Profiles AS P1 
  LEFT
  JOIN Products AS P2 
    ON P1.id = P2.profileid 
 GROUP 
    BY P1.id
     , P1.name 
 ORDER 
    BY profileid ASC
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜