0

我有一个 MYSQL 查询,他必须列出我希望它发布的所有帖子。但它不这样做。当我在表“meaOrder”中有一个以上具有相同“ordCode”的帖子时,它会显示帖子。但是当我只在 meaOrder 中发布时,我不会显示它。我能做些什么?

SELECT koden, wish, rnamn, bild, pris, cname, onsktext 
                FROM (
                SELECT m.wishText as onsktext, m.meaOID as midn, m.ordcode as koden, w.wish as 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=4401
                    WHERE  c.ordStatus=1 AND 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 midn DESC

TABLE 订单 http://grab.by/m74E

表 meaOrder http://grab.by/m74Q

4

2 回答 2

2

在这种情况下,请尝试将 JOIN 替换为 RIGHT JOIN。MySQL 文档中的 JOIN Syntax 页面解释了不同之处。简而言之 - JOIN 仅当两个连接表中都有相应的行(内部连接)时才返回行。LEFT JOIN / RIGHT JOIN 返回一个表中的所有行,如果存在来自另一个表的相应行(这些是外连接)

于 2013-04-30T16:00:41.413 回答
0

你需要一个子选择吗?

这似乎涵盖了它:-

SELECT m.ordcode AS koden, w.wish AS wish, r.meaName AS rnamn, r.meaImg AS bild, r.meaPrice AS pris, k.catName AS cname, m.wishText AS onsktext 
FROM cats k 
INNER JOIN meals r ON r.catID = k.catID
INNER JOIN meaOrder m ON m.meaID = r.meaID 
INNER JOIN wishes w ON m.wishesID = w.id
INNER JOIN orders c ON m.ordNR = c.ordNR
WHERE  c.ordStatus = 1  
AND c.ordNR = 4401
GROUP BY m.ordcode, r.meaName, r.meaImg, r.meaPrice, k.catName
ORDER BY midn DESC
于 2013-04-30T16:09:21.367 回答