许多数据以一个记录MYSQL

时间:2012-10-25 23:14:11

标签: mysql record

我有一个名为medOrder的表。在这张表中,我有订单。一个订单有很多愿望。我试着编写一个显示一条记录和所有愿望名称的查询,但我认为查询不尽如人意。

SELECT m.ordcode as koden, w.wish, r.meaName as rnamn, r.meaImg as bild, 
        r.meaPrice as pris, k.catName as cname from cats k, meals r, wishes w,
        meaOrder m
    join orders c on c.ordNR='7265'
    WHERE m.ordNR=c.ordNR AND m.meaID=r.meaID AND m.wishesID=w.id
        AND r.catID=k.catID
    ORDER BY m.ordcode DESC;

data schema

1 个答案:

答案 0 :(得分:1)

尝试此查询:

SELECT koden, GROUP_CONCAT(wish) wish, rnamn, bild, pris, cname 
FROM
(
SELECT m.ordcode as koden, w.wish, r.meaName as rnamn, r.meaImg as bild, 
        r.meaPrice as pris, k.catName as cname from cats k, meals r, wishes w,
        meaOrder m
    join orders c on c.ordNR='7265'
    WHERE m.ordNR=c.ordNR AND m.meaID=r.meaID AND m.wishesID=w.id
        AND r.catID=k.catID
) T
GROUP BY koden, rnamn, bild, pris, cname
ORDER BY koden DESC

此查询对所有重复列执行GROUP BY,但连接wish的非重复列的值。