0

好的,我想做的是尝试从一个跨越 4 个单独列的表中获取唯一值。

例如,我有以下查询,它可以在其中一列上正常工作。

SELECT 
    rest.cuisine1, c.name
FROM
    specials
    INNER JOIN restaurant AS rest ON specials.restaurantid=rest.id
    INNER JOIN cuisine AS c ON rest.cuisine1=c.id
WHERE
    dateend >= CURDATE()
AND
    (specials.state='VIC' OR specials.state = 'ALL') 
AND 
    specials.status = 1 
AND 
    rest.status = 1
GROUP BY
    c.id;

现在,rest.cuisine1是包含数据的列之一。正如预期的那样,此查询仅从该列返回唯一值。以下是返回的示例:

12 Cafe
18 Asian
29 Coffee

该表中还有 3 列,它们是:

rest.cuisine2
rest.cuisine3
rest.cuisine4

我可以运行上述查询 4 ​​次(每列一个),然后通过 PHP 运行值以仅从 4 个不同的结果集中获取唯一值,但是我想知道是否可以在一个查询。

4

2 回答 2

1

尝试这个

 SELECT 
    rest.cuisine1, c.name ,rest.cuisine2 , rest.cuisine3, rest.cuisine4
FROM
    specials
    INNER JOIN restaurant AS rest ON specials.restaurantid=rest.id
    INNER JOIN cuisine AS c ON rest.cuisine1=c.id
            INNER JOIN cuisine AS c2 ON rest.cuisine2=c2.id
            INNER JOIN cuisine AS c3 ON rest.cuisine3=c3.id
            INNER JOIN cuisine AS c4 ON rest.cuisine4=c4.id
WHERE
    dateend >= CURDATE()
AND
    (specials.state='VIC' OR specials.state = 'ALL') 
AND 
    specials.status = 1 
AND 
    rest.status = 1
GROUP BY
    c.id;
于 2012-12-16T12:32:43.373 回答
0

该答案基于 MahmoudGamal 在评论中发布的答案,他出于某种原因将其删除。

我用下面的..

SELECT 
    c.id, c.name
FROM
    specials
    INNER JOIN restaurant AS rest ON specials.restaurantid=rest.id
    INNER JOIN cuisine AS c ON c.id IN (rest.cuisine1, rest.cuisine2, rest.cuisine3, rest.cuisine4)
WHERE
    dateend >= CURDATE()
AND
    (specials.state='VIC' OR specials.state = 'ALL') 
AND 
    specials.status = 1 
AND 
    rest.status = 1
GROUP BY
    c.id;
于 2012-12-16T12:59:59.600 回答