[英]How to write SQL to transform a table so that rows become columns and vice-versa
This is my table: 这是我的桌子:
+--------+-----------+
|country |city |
+--------+-----------+
| india | hydrabad |
| india | bangalore |
| india | mumbai |
| UK | London |
| UK | france |
| UK | Conberry |
| USA | new jersy |
| USA | texas |
| USA | New YORK |
+--------+-----------+
I want to show it like this: 我想这样显示:
+--------+----------+--------+------------+
|country |city1 |city2 |city3 |
+--------+----------+--------+------------+
| india | hydrabad | mumbai | Bangalore |
| UK | London | France | Conberry |
| USA | newjersy | texas | New YORK |
+--------+----------+--------+------------+
How can I write a MySQL query to do this? 如何编写MySQL查询来执行此操作?
you can use a query like this: 您可以使用如下查询:
SELECT country,
GROUP_CONCAT(IF(nr=0,city,NULL)) AS city1,
GROUP_CONCAT(IF(nr=1,city,NULL)) AS city2,
GROUP_CONCAT(IF(nr=2,city,NULL)) AS city3
FROM (
SELECT
@nr := IF(@country_last = c.country, @nr:=(@nr+1),@nr:=0) AS nr,
@country_last := c.country AS country,
c.city
FROM country c
CROSS JOIN (SELECT @nr := 0, @country_last := '') param
ORDER BY c.country
) AS d
GROUP BY country;
sample 样品
MariaDB [yourschema]> select * from country;
+---------+-----------+
| country | city |
+---------+-----------+
| india | hydrabad |
| india | bangalore |
| india | mumbai |
| uk | London |
| uk | france |
| uk | Conberry |
+---------+-----------+
6 rows in set (0.00 sec)
MariaDB [yourschema]>
MariaDB [yourschema]> SELECT country,
-> GROUP_CONCAT(IF(nr=0,city,NULL)) AS city1,
-> GROUP_CONCAT(IF(nr=1,city,NULL)) AS city2,
-> GROUP_CONCAT(IF(nr=2,city,NULL)) AS city3
-> FROM (
-> SELECT
-> @nr := IF(@country_last = c.country, @nr:=(@nr+1),@nr:=0) AS nr,
-> @country_last := c.country AS country,
-> c.city
-> FROM country c
-> CROSS JOIN (SELECT @nr := 0, @country_last := '') param
-> ORDER BY c.country
-> ) AS d
-> GROUP BY country;
+---------+----------+-----------+----------+
| country | city1 | city2 | city3 |
+---------+----------+-----------+----------+
| india | hydrabad | bangalore | mumbai |
| uk | London | france | Conberry |
+---------+----------+-----------+----------+
2 rows in set (0.00 sec)
MariaDB [yourschema]>
Use join query for it( If cities are fixed) 使用联接查询(如果城市固定)
SELECT A.country,A.city city1,B.city city2,C.city city3 FROM tblname A
LEFT JOIN tblname B ON A.country=B.country
LEFT JOIN tblname C ON A.country=C.country
WHERE A.city!=B.city and A.city!=C.city and B.city!=C.city
GROUP BY A.country
If you want to use all cities separated by comma in single column use like this, 如果您想在单列中使用以逗号分隔的所有城市,请按以下方式使用,
SELECT country, GROUP_CONCAT(city) as Cities FROM tblname GROUP BY country
Try this query, 试试这个查询,
Select Country, City1, City2, City3
From
(
Select Country, City,
'City'+
cast(row_number() over(partition by Country order by Country)
as varchar(10)) ColumnSequence
From Country
) Temp
Pivot
(
Max(City)
For ColumnSequence in (City1, City2, City3)
) Piv
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.