0

我想结合以下两个查询,谁能给我一个想法如何做到这一点?(仅当查询有结果INSERT时才应运行查询)SELECT

查询 1

INSERT IGNORE INTO senders(
sender_id,
telephone,
)
VALUES ( 1, 0723355888)

查询 2

SELECT student_name
FROM students
WHERE student_id =1
4

3 回答 3

2
INSERT INTO senders (sender_id, telephone)
SELECT student_id, student_telephone FROM students
WHERE student_id = 1
LIMIT 1

或者如果电话不是学生表的一部分,则对其进行硬编码:这是有效的,因为如果查询 0 行,则根本不执行任何插入。

INSERT INTO senders (sender_id, telephone)
SELECT 1, 0723355888 FROM students
WHERE student_id = 1
LIMIT 1
于 2012-05-24T11:26:24.910 回答
0

尝试使用此查询 -

INSERT IGNORE INTO senders (sender_id, telephone)  
SELECT student_id as sender_id, telephone
FROM students
WHERE student_id=1
于 2012-05-24T11:30:20.387 回答
0

尝试这个:

INSERT IGNORE INTO senders( sender_id, telephone) 
SELECT 1 as sender_id, 0723355888 as telephone 
FROM students WHERE student_id =1
于 2012-05-24T11:30:25.623 回答