Mysql选择不同的多列循环结果

时间:2014-11-12 18:36:43

标签: php mysql

我正在尝试从2个不同的列中选择没有重复的类别,该表看起来像这样:

cuisine_1    cuisine_2
----------------------
italian       french
italian       chinese
japanese      german
western
french        german
international
western       sushi
sushi         steak
steak
chinese
vietnamese    chinese

如果来自cuisine_1cuisine_2

,则预期结果仅为每个类别一次无关紧要

我试过这样:

$categories=mysqli_query($link,"select distinct cuisine_1,cuisine_2 AS cuisine_unique from table");


while($row_cu=mysqli_fetch_array($categories)){
       echo $row_cu['cuisine_unique']."<br>";
    }

到目前为止没有任何运气。我是否必须先对结果进行分组,或者我做错了什么?

2 个答案:

答案 0 :(得分:2)

您可以尝试使用UNION

(SELECT distinct cuisine_1 AS cuisine_unique FROM table) UNION (SELECT distinct cuisine_2 FROM table)

SqlFiddle

答案 1 :(得分:0)

问题是,如果您按cuisine_1进行过滤,则会丢失其他列中的某些数据,因此您可以尝试为每列获取一个SELECT DISTINCT,然后合并两个数组并过滤array_unique

相关问题