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

Hvordan opdeles kommasepareret række af poster og derefter arrangeres sekventielt i MySQL?

Du kan bruge MySQL SUBSTRING_INDEX(). Det vil returnere understrengen fra den givne kommaseparerede streng før et specificeret antal forekomster af afgrænsningstegnet.

Prøv dette, det ser ud til at fungere fint:

SELECT ID ,SUBSTRING_INDEX(SUBSTRING_INDEX(t.AgentID, ',', n.n), ',', -1) Agent ,Name ,SUBSTRING_INDEX(SUBSTRING_INDEX(t.Return_Date, ',', n.n), ',', -1) Return_DateFROM table1 t CROSS JOIN (VÆLG a.N + b.N * 10 + 1 n FRA (VÆLG 0 SOM N UNION ALLE VÆLG 1 UNION ALLE VÆLG 2 UNION ALLE VÆLG 3 UNION ALLE VÆLG 4 UNION ALLE VÆLG 5 UNION ALLE VÆLG 6 UNION ALLE VÆLG 7 UNION ALLE VÆLG 8 UNION ALLE VÆLG 9) a ,(VÆLG 0 SOM N UNION ALLE VÆLG 1 UNION ALLE VÆLG 2 UNION ALLE VÆLG 3 UNION ALLE VÆLG 4 UNION ALLE VÆLG 5 UNION ALLE VÆLG 6 UNION ALLE VÆLG 7 UNION ALL SELECT 8 UNION ALL SELECT 9) b ORDER BY n ) nWHERE n.n <=1 + (LENGTH(t.Return_Date) - LENGTH(REPLACE(t.Return_Date, ',', '')))ORDER BY ID;

Tjek dette..SQL Fiddle HER

For yderligere undersøgelse gå på MySQL Split String Function



  1. MySQL match() mod() - rækkefølge efter relevans og kolonne?

  2. Topteknikker til at undgå 'dataskrabning' fra en hjemmesidedatabase

  3. Hvordan vælger man 1d array fra 2d array?

  4. Hvordan administrerer man brugerroller i en database?