在 mysql 中如何根据一组给定的值为 table 中的每一行设置一列
in mysql how to set a column for every row in a table from a give set of values
我有这个table:
id name code
---------------------------
1 n1
2 n2
3 n3
我有这些值需要写入每一行的代码列:
('8n8kKaVu','SRE2vbpQ','Vdfb7V7s')
此外,上面 table 中的值设置到哪一行并不重要。所以假设我希望最终的 table 看起来像这样:
id name code
-----------------------
1 n1 8n8kKaVu
2 n2 SRE2vbpQ
3 n3 Vdfb7V7s
如果将所有代码加载到一个 table 中,您可以创建一个自动递增的密钥:
alter table codes add id integer auto_increment key;
然后用代码 table:
中的值更新原始 table 中的代码列
UPDATE original_table
SET code = (SELECT
codes.code
FROM codes
WHERE original_table.id = codes.id);
我有这个table:
id name code
---------------------------
1 n1
2 n2
3 n3
我有这些值需要写入每一行的代码列:
('8n8kKaVu','SRE2vbpQ','Vdfb7V7s')
此外,上面 table 中的值设置到哪一行并不重要。所以假设我希望最终的 table 看起来像这样:
id name code
-----------------------
1 n1 8n8kKaVu
2 n2 SRE2vbpQ
3 n3 Vdfb7V7s
如果将所有代码加载到一个 table 中,您可以创建一个自动递增的密钥:
alter table codes add id integer auto_increment key;
然后用代码 table:
中的值更新原始 table 中的代码列UPDATE original_table
SET code = (SELECT
codes.code
FROM codes
WHERE original_table.id = codes.id);