2012-05-24 106 views
0

我想結合以下兩個查詢,任何人都可以給我一個想法如何做到這一點? (該INSERT查詢應該運行僅當SELECT查詢有一個結果)如何將SELECT + WHERE查詢與INSERT查詢結合使用?

查詢沒有1

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

查詢沒有2

SELECT student_name 
FROM students 
WHERE student_id =1 
+0

爲什麼ü想如果u r不將其插入到 '發件人',選擇 'student_name'? – Addicted

回答

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

或如果telefone不是學生表的一部分,則很難c頌詞: 這適用,因爲如果查詢0行,根本不執行插入。

INSERT INTO senders (sender_id, telephone) 
SELECT 1, 0723355888 FROM students 
WHERE student_id = 1 
LIMIT 1 
+0

感謝它的工作。 –

0

嘗試使用此查詢 -

INSERT IGNORE INTO senders (sender_id, telephone) 
SELECT student_id as sender_id, telephone 
FROM students 
WHERE student_id=1 
0

試試這個:

INSERT IGNORE INTO senders(sender_id, telephone) 
SELECT 1 as sender_id, 0723355888 as telephone 
FROM students WHERE student_id =1