Jak dodać ciąg znaków do wartości kolumny w MySQL?
Potrzebuję instrukcji SQL update do aktualizacji określonego pola wszystkich wierszy z ciągiem "test", który ma być dodany przed istniejącą wartością.
Na przykład, jeśli istniejącą wartością jest "try", powinna ona stać się "testtry".
104
Author: Spontifixus, 2009-03-25
5 answers
Możesz użyć CONCAT w tym celu:
UPDATE tbl SET col=CONCAT('test',col);
Jeśli chcesz uzyskać mądrzejsze i aktualizować tylko kolumny, które nie mają jeszcze poprzedzonych testów, spróbuj
UPDATE tbl SET col=CONCAT('test',col)
WHERE col NOT LIKE 'test%';
228
Author: Paul Dixon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-25 10:06:59
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-25 10:06:59
UPDATE tablename SET fieldname = CONCAT("test", fieldname) [WHERE ...]
15
Author: Ferdinand Beyer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-25 09:18:43
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-25 09:18:43
To proste
UPDATE YourTable SET YourColumn = CONCAT('prependedString', YourColumn);
6
Author: soulmerge,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-25 09:25:27
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-25 09:25:27
Wiele funkcji aktualizacji ciągów w MySQL wydaje się działać tak:
Jeśli jednym argumentem jest null
, to konkatenacja lub inne funkcje zwracają również null
.
Tak więc, aby zaktualizować pole o wartość null
, najpierw ustaw je na wartość inną niż null, na przykład ''
Na przykład:
update table set field='' where field is null;
update table set field=concat(field,' append');
5
Author: bvidinli,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-04 13:57:38
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-04 13:57:38
- UPDATE table_name SET Column1 = CONCAT ('newtring', table_name.Column1) where 1
- UPDATE table_name SET Column1 = CONCAT ('newtring', table_name.Column2) where 1
- UPDATE table_name SET Column1 = CONCAT ('newtring', table_name.Column2, 'newtring2') gdzie 1
Możemy połączyć tę samą kolumnę lub inną kolumnę tabeli.
0
Author: user3419778,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-14 12:41:40
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-14 12:41:40