Using GROUP_CONCAT on subquery in MySQL

前端 未结 5 1703
我在风中等你
我在风中等你 2021-02-02 06:28

I have a MySQL query in which I want to include a list of ID\'s from another table. On the website, people are able to add certain items, and people can then add those items to

5条回答
  •  臣服心动
    2021-02-02 06:38

    OP almost got it right. GROUP_CONCAT should be wrapping the columns in the subquery and not the complete subquery (I'm dismissing the separator because comma is the default):

    SELECT i.*,
    (SELECT GROUP_CONCAT(userid) FROM favourites f WHERE f.itemid = i.id) AS idlist
    FROM items i
    WHERE i.id = $someid
    

    This will yield the desired result and also means that the accepted answer is partially wrong, because you can access outer scope variables in a subquery.

提交回复
热议问题