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

MySQL feltdataparsing

Jeg tror, ​​du har brug for en mere sofistikeret måde at bekræfte, at Models kolonneværdier er korrekte på grund af mellemrum efter semikolon. Desuden, hvis du har brug for at behandle Models kolonneværdier, der indeholder mere end ti dele, skal du bruge en længere talrække. Jeg bruger INFORMATION_SCHEMA COLUMNS bord til dette. Du kan også oprette en hjælpetabel til dette.

SELECT CONCAT( IF(N.n = 0, '', LEFT(t.part, 2)), SUBSTRING_INDEX( SUBSTRING_INDEX(t.part, '-', N.n + 1), '-', -1 ) ) AS part, t.isHD, t.ID, t.GroupNumber, t.PartNo, t.Models FROM Numbers AS N CROSS JOIN ( SELECT SUBSTRING_INDEX( SUBSTRING_INDEX(t.part, ' ', N.n + 1), ' ', -1 ) AS part, t.isHD, t.ID, t.GroupNumber, t.PartNo, t.Models FROM Numbers AS N CROSS JOIN ( SELECT SUBSTRING_INDEX( SUBSTRING_INDEX(t.part, ';', N.n + 1), ';', -1 ) AS part, t.isHD, t.ID, t.GroupNumber, t.PartNo, t.Models FROM Numbers AS N CROSS JOIN ( SELECT REPLACE( SUBSTRING_INDEX( SUBSTRING_INDEX(t.part, 'LHD', N.n + 1), 'LHD', -1 ), ',', '' ) AS part, IF(N.n > 0, 1, t.isHD) AS isHD, t.ID, t.GroupNumber, t.PartNo, t.Models FROM Numbers AS N CROSS JOIN ( SELECT SUBSTRING_INDEX( SUBSTRING_INDEX(p.Models, 'RHD', N.n + 1), 'RHD', -1 ) AS part, IF(N.n > 0, 2, 0) AS isHD, p.ID, p.GroupNumber, p.PartNo, p.Models FROM Numbers AS N CROSS JOIN parts_listing AS p WHERE p.Models REGEXP '[[:<:]]([LR]HD,? +)?[0-9]+(-[0-9]+)*[[:>:]]' AND N.n <= (LENGTH(p.Models) - LENGTH(REPLACE(p.Models, 'RHD', ''))) / 3 ) AS t WHERE N.n <= (LENGTH(t.part) - LENGTH(REPLACE(t.part, 'LHD', ''))) / 3 ) AS t WHERE N.n <= LENGTH(t.part) - LENGTH(REPLACE(t.part, ';', '')) ) AS t WHERE N.n <= LENGTH(t.part) - LENGTH(REPLACE(t.part, ' ', '')) ) AS t WHERE t.part REGEXP '^[0-9]+(-[0-9]+)*$' AND N.n <= LENGTH(t.part) - LENGTH(REPLACE(t.part, '-', ''));

Delvis output (29 rækker):

+------+------+----+-------------+---------+--------------------------------------+ | part | isHD | ID | GroupNumber | PartNo | Models | +------+------+----+-------------+---------+--------------------------------------+ | 2201 | 0 | 1 | 0.000 | 391906 | 2201-02-11-20-22-32-40; 2301 | | 2202 | 0 | 1 | 0.000 | 391906 | 2201-02-11-20-22-32-40; 2301 | | 2211 | 0 | 1 | 0.000 | 391906 | 2201-02-11-20-22-32-40; 2301 | | 2220 | 0 | 1 | 0.000 | 391906 | 2201-02-11-20-22-32-40; 2301 | | 2222 | 0 | 1 | 0.000 | 391906 | 2201-02-11-20-22-32-40; 2301 | | 2232 | 0 | 1 | 0.000 | 391906 | 2201-02-11-20-22-32-40; 2301 | | 2240 | 0 | 1 | 0.000 | 391906 | 2201-02-11-20-22-32-40; 2301 | | 2301 | 0 | 1 | 0.000 | 391906 | 2201-02-11-20-22-32-40; 2301 | | 2213 | 1 | 3 | 0.000 | 399391 | LHD 2213 | | 2313 | 2 | 5 | 0.000 | 415245 | RHD, 2313 | | 2631 | 0 | 7 | 0.000 | 436803 | 2631 (BODY 2678); 5431 (BODY 5478) | | 5431 | 0 | 7 | 0.000 | 436803 | 2631 (BODY 2678); 5431 (BODY 5478) | | 2206 | 1 | 14 | 0.0015 | G179559 | LHD, 2206-13-26-33; 2302-06-13-32-33 | | 2213 | 1 | 14 | 0.0015 | G179559 | LHD, 2206-13-26-33; 2302-06-13-32-33 | | 2226 | 1 | 14 | 0.0015 | G179559 | LHD, 2206-13-26-33; 2302-06-13-32-33 | | 2233 | 1 | 14 | 0.0015 | G179559 | LHD, 2206-13-26-33; 2302-06-13-32-33 | | 2302 | 1 | 14 | 0.0015 | G179559 | LHD, 2206-13-26-33; 2302-06-13-32-33 | | 2306 | 1 | 14 | 0.0015 | G179559 | LHD, 2206-13-26-33; 2302-06-13-32-33 | | 2313 | 1 | 14 | 0.0015 | G179559 | LHD, 2206-13-26-33; 2302-06-13-32-33 | | 2332 | 1 | 14 | 0.0015 | G179559 | LHD, 2206-13-26-33; 2302-06-13-32-33 | | 2333 | 1 | 14 | 0.0015 | G179559 | LHD, 2206-13-26-33; 2302-06-13-32-33 | | 2201 | 0 | 15 | 0.0015 | 422024 | 2201-02-11-20-22-32-40; RHD, 2301 | | 2202 | 0 | 15 | 0.0015 | 422024 | 2201-02-11-20-22-32-40; RHD, 2301 | | 2211 | 0 | 15 | 0.0015 | 422024 | 2201-02-11-20-22-32-40; RHD, 2301 | | 2220 | 0 | 15 | 0.0015 | 422024 | 2201-02-11-20-22-32-40; RHD, 2301 | | 2222 | 0 | 15 | 0.0015 | 422024 | 2201-02-11-20-22-32-40; RHD, 2301 | | 2232 | 0 | 15 | 0.0015 | 422024 | 2201-02-11-20-22-32-40; RHD, 2301 | | 2240 | 0 | 15 | 0.0015 | 422024 | 2201-02-11-20-22-32-40; RHD, 2301 | | 2301 | 2 | 15 | 0.0015 | 422024 | 2201-02-11-20-22-32-40; RHD, 2301 | +------+------+----+-------------+---------+--------------------------------------+

Demo på DB Fiddle .



  1. Hvordan forbinder man IntelliJ med lokal MySQL?

  2. Eksporter MySQL-database ved hjælp af PHP

  3. Mysql:Trim alle felter i databasen

  4. Sådan gemmer du en UUID som binær(16) i java