我有两张桌子:
CREATE TABLE Table1 (
ID VARCHAR(15),
Value INT
);
INSERT INTO Table1 (`ID`, `Value`)
VALUES
('A', 21),
('B', 50),
('C', 10);
CREATE TABLE Table2 (
ID VARCHAR(15)
);
INSERT INTO Table2 SELECT ID FROM Table1;
我必须重命名Table2中的所有ID,其值在table1< 20到“Low_< ID Name>”。在此之后,“Table2”应如下所示:
mysql> SELECT ID FROM Table2 +-------+ | ID | +-------+ | A | | B | | Low_C | +-------+
什么查询会这样做?
答案 0 :(得分:6)
UPDATE Table2
LEFT JOIN Table1 ON Table2.ID=Table1.ID
SET Table2.ID = CONCAT('Low_',Table2.ID)
WHERE Table1.Value<20
但正如@Phil所说......你确定要改变你的(推测)主键ID
吗?
答案 1 :(得分:2)
UPDATE Table2 SET ID = CONCAT('Low_', ID)
WHERE ID IN (SELECT ID FROM Table1 WHERE `Value` < 20)
答案 2 :(得分:1)
也许像
UPDATE Table2 SET ID = "Low_" + ID
WHERE ID IN (SELECT ID FROM Table1 WHERE Value < 20)