sql >> Database >  >> RDS >> Mysql

Zoek tekstpositie, extraheer tekst en voeg in nieuwe kolom in MySQL

Je kunt de tekst kiezen na de 'Country=', en als je die substring hebt, kies je de tekst vóór de eerste '&'

SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(ColumnA, 'Country=', -1), '&', 1) AS ColumnB
FROM `atable`

Zie http://dev.mysql. com/doc/refman/5.6/en/string-functions.html#function_substring-index

Hier is een test om te demonstreren:

mysql> SELECT * FROM atable;
+------+------------------------------------------+
| row  | columna                                  |
+------+------------------------------------------+
| Row1 | Lauguage=English&Country=USA&Gender=Male |
| Row2 | Gender=Female&Language=French&Country=   |
| Row3 | Country=Canada&Gender=&Language=English  |
+------+------------------------------------------+

mysql> SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(ColumnA, 'Country=', -1), '&', 1) AS ColumnB FROM atable;
+---------+
| ColumnB |
+---------+
| USA     |
|         |
| Canada  |
+---------+

Over uw vervolgvraag:

INSERT INTO atable VALUES ('Row4', 'Gender=&Language=English');

SELECT `row`, IF(LOCATE('Country=', ColumnA)>0, 
  COALESCE(
    NULLIF(SUBSTRING_INDEX(SUBSTRING_INDEX(ColumnA, 'Country=', -1), '&', 1), ''), 
    'Blank string is not valid!'), 
 'Missing Country!') AS ColumnB     
FROM `atable`

+------+----------------------------+
| row  | ColumnB                    |
+------+----------------------------+
| Row1 | USA                        |
| Row2 | Blank string is not valid! |
| Row3 | Canada                     |
| Row4 | Missing Country!           |
+------+----------------------------+


  1. EM12c staat nu DB12c toe voor repo's

  2. PHP MYSQL UPDATE indien aanwezig of INSERT indien niet?

  3. Rails-ontwikkeling - Kan geen verbinding maken met MySQL-server op 'localhost' (10061)

  4. Importeren van 8GB mysql dump duurt lang