0
INSERT INTO `dictionary2` (word,verb)
SELECT SUBSTRING(word FROM 2)
FROM `dictionary1`
WHERE `dictionary1`.word LIKE "w%"

我有两个表,dictionary1(word) 和 dictionary2(word,verb)。

  • 我想将来自dictionary1的值插入dictionary2,其中单词以'w'开头,并且dictionary2.word中不存在该值。
  • 在同一个插入中,我想将 dictionary2.verb 的值设置为 1。
4

2 回答 2

1

你可以使用这个:

INSERT INTO dictionary2 (word, verb)
SELECT dictionary1.word, 1
FROM
  dictionary1 LEFT JOIN dictionary2
  ON dictionary1.word = dictionary2.word
WHERE
  dictionary1.word LIKE 'w%'
  AND dictionary2.word IS NULL

在此处查看小提琴。

于 2013-03-11T22:42:21.507 回答
0

试试这个:

SQL查询:

INSERT INTO `dictionary2` (word,verb)
SELECT word, (select @number :=1)
FROM `dictionary1`
WHERE `dictionary1`.word LIKE "t%"
AND `dictionary1`.word NOT IN(SELECT word FROM dictionary2);

样本数据:

CREATE TABLE `dictionary2` (
ID INT AUTO_INCREMENT PRIMARY KEY,
word VARCHAR (50),
verb VARCHAR (50)
);

CREATE TABLE `dictionary1` (
ID INT AUTO_INCREMENT PRIMARY KEY,
word VARCHAR (50)
);

INSERT INTO `dictionary2`(word,verb) 
VALUES
('test','test'),
('test1','test1'); 
INSERT INTO `dictionary1`(word) VALUES('test'),('testing1');

顺便说一句,您可以安全地更改LIKE "t%"LIKE "w%";-)

SQL 小提琴演示

于 2013-03-11T22:42:06.443 回答