1:在mysql查询中实现N.

时间:2015-09-03 06:09:58

标签: php mysql

假设表state_info中有2列。

state       |  district
------------+---------------
Karnataka   |  Bangalore
Karnataka   |  Belgium
Karnataka   |  Mysore
Mumbai      |  Sandra
Mumbai      |  Madeira

我想在一个列中输出它们

Karnataka
Bangalore
Belgium
Mysore

Mumbai
Sandra
Madeira

低于它的dist,再次说明它的dist

3 个答案:

答案 0 :(得分:0)

使用

SELECT CONCAT(state, ', ', district) FROM state_info;

获取

Karnataka, Bangalore
Karnataka, Belgium
Karnataka, Mysore
Mumbai, Sandra
Mumbai, Madeira

答案 1 :(得分:0)

你可以这样做

Select Distinct(state) from Table1
UNION 
Select Distinct(district) from Table1;

结果将是

Bangalore
Belgium
Mysore
Sandra
Madeira
Karnataka
Mumbai

答案 2 :(得分:0)

select * from (
 Select Distinct(state) as data from Table1
 UNION 
 Select Distinct(district) as data from Table1;
) order by  data