好的,我要做的是尝试从一个跨越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个不同的结果集中获取唯一值,但是我想知道我是否能得到我想要的全部在一个查询中。
答案 0 :(得分: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;
答案 1 :(得分: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;