sql >> Database teknologi >  >> RDS >> Mysql

Kombiner flere underordnede rækker til én række MYSQL uden hardkodning eller min/max-værdier

Du kan bruge nedenstående forespørgsel-

SELECT a.PK , a.Name, a.Email, SUBSTRING_INDEX(GROUP_CONCAT(i.Image_Name),',',1) AS Image_name_1, IF((LENGTH(GROUP_CONCAT(i.Image_Name)) - LENGTH(GROUP_CONCAT(i.Image_Name SEPARATOR ''))) < 1,'',SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(i.Image_Name),',',2),',',-1)) AS Image_name_2, IF((LENGTH(GROUP_CONCAT(i.Image_Name)) - LENGTH(GROUP_CONCAT(i.Image_Name SEPARATOR ''))) < 2,'',SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(i.Image_Name),',',3),',',-1),',',-1)) AS Image_name_3, IF((LENGTH(GROUP_CONCAT(i.Image_Name)) - LENGTH(GROUP_CONCAT(i.Image_Name SEPARATOR ''))) < 3,'',SUBSTRING_INDEX(GROUP_CONCAT(i.Image_Name),',',-1)) AS Image_name_4 FROM `advertiser` a LEFT JOIN category c ON a.PK = c. FK LEFT JOIN images I ON a.PK = i.FK WHERE c.FK = 1 GROUP BY a.PK, a.Name,a.Email;

  1. Sjove tweets om en DBA's liv

  2. Udvikling af PostgreSQL til Windows, del 2

  3. delvis resultater fra en langvarig SELECT-forespørgsel?

  4. Sådan gør du MERGE serialiserbar