This is my items table in mysql
----------- -------------
| id | k_id |
----------- -------------
| 1 | 12,2,65,98 |
----------- -------------
| 2 | 2,5,77,234 |
----------- -------------
| 3 | 48,125,119 |
----------- -------------
And this table has 120000 rows.
now I want to change all values in k_id
column like this one:
----------- ---------------------
| id | k_id |
----------- ---------------------
| 1 | "12","2","65","98" |
----------- ---------------------
| 2 | "2","5","77","234" |
----------- ---------------------
| 3 | "48","125","119" |
----------- ---------------------
How can do this? Can do this using php? Can do this in phpmyadmin in cpanel?
please help me with best way.
没有检查,但是像这样
UPDATE tablename SET k_id = CONCAT('"', REPLACE(k_id, ',', '","'), '"')
UPDATE tablename SET k_id = CONCAT('"', REPLACE(k_id,',','","'), '"');
I'm assuming you want the data in separate strings like that so that you can use it in an IN() statement... its not necessary as MySQL has a method to handle that exact type of data (aka comma separated list)
SELECT id FROM table WHERE FIND_IN_SET('5', k_id) > 0;
this will return id 3.. however I would recommend you normalize your database... storing comma separated values is always a bad idea..
CREATE TEMPORARY TABLE IF NOT EXISTS normalized_table AS
( SELECT
id,
SUBSTRING_INDEX(SUBSTRING_INDEX(k_id, ',', n.digit+1), ',', -1) column2
FROM test
JOIN(SELECT 0 digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4) n
ON LENGTH(REPLACE(k_id, ',' , '')) <= LENGTH(k_id)-n.digit
ORDER BY id, n.digit
);
DROP table test; -- test is just a dummy name for the name of your table
CREATE table test (id int, k_id int);
INSERT INTO test (id, k_id)
SELECT id, k_id FROM normalized_table;
在MySQL中使用此Use Concat函数并替换函数
update TableName set k_id=CONCAT('"', REPLACE(',', '","', k_id, '"');
Another way would be to get all the values, do the string manipulation in PHP, and reinsert - but I like Cheery's way better. Though if you need to put your integers into strings in your database, it sounds like you might have a different problem somewhere else that could be addressed without changing all the values like this.
try
UPDATE my_table SET k_id = CONCAT('"', REPLACE(k_id, ',', '","'), '"')
UPDATE my_table SET k_id = CONCAT( k_id, ", ", "999") ;
//Use the function mysql CONCAT_WS() -> Concatenate With Separator
UPDATE TABLE_NAME SET FIELD_NAME = CONCAT_WS(",",k_id)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.