sql >> Databasteknik >  >> RDS >> Mysql

Kombinera flera underordnade rader till en rad MYSQL utan hårdkodning eller min/max-värden

Du kan använda nedanstående fråga-

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. Se om lat / long faller inom en polygon med mysql

  2. Bygg infogningsfråga från array MySQL och PHP

  3. Anslut till SQL Server 2012 Database med C# (Visual Studio 2012)

  4. Bevilja rättigheter till ytterligare MySQL-databas som orsakar problem