0

我有一个名为 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;

数据模式

4

1 回答 1

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

于 2012-10-25T23:27:48.157 回答