2010-04-04 23 views
1
INSERT INTO item_quotation 
(item_id, quotation_id,name_searched,item_name,other_name,selling_price,discounted_price) 
SELECT DISTINCT I.item_id," . $quotation_id . ",T.item_name, I.name,I.other_name, INV.selling_price, I.discounted_price 
FROM temp_quotations T, item I, inventory INV<br/> 
WHERE (I.name LIKE CONCAT( '%', T.item_name, '%') 
OR I.other_name LIKE CONCAT( '%', T.item_name, '%')) 
AND INV.item_id = I.item_id; 

我有一個表稱爲temp_quotations(temp_item_id,ITEM_NAME)具有值
(1,粉碎機),(2,鑽頭),(3,試劑盒)
我有稱爲項的另一表(item_id,name,other_name,discounted_price)具有值
(1,grinder A,100),(2,kit A,200)
當我執行此sql時,它將值插入表item_quotation(item_id,quotation_id,name_searched ,item_name,other_name,selling_price,discounted_price)例如在這種情況下,它將插入
(1,1,研磨機,研磨機A,150,100)
(2,1,試劑盒,試劑盒A,250,200)
由於項目鑽取在表格項目中找不到,因此不會在表格item_quotations中顯示它。我想在該sql中編寫一條if語句,以便當temp_quotation中的item_name與item中的名稱不匹配時,它不會在item_name的位置找到。 這是我試過,但它並沒有顯示「未找到」:IF在SQL表達式

INSERT INTO item_quotation 
(item_id, quotation_id,name_searched,item_name,other_name,selling_price,discounted_price) 
SELECT DISTINCT I.item_id," . $quotation_id . ",T.item_name, 
IF(I.name LIKE CONCAT( '%', T.item_name, '%'),I.name,'not found'),I.other_name, INV.selling_price, I.discounted_price 
FROM temp_quotations T, item I, inventory INV 
WHERE (I.name LIKE CONCAT( '%', T.item_name, '%') 
OR I.other_name LIKE CONCAT( '%', T.item_name, '%')) 
AND INV.item_id = I.item_id; 

回答

0

那麼,你可以爲MySQL 創建函數或過程的語法應該是這樣的:

CREATE PROCEDURE insert_item(IN ITEM STRING) 
BEGIN 
SELECT item_id FROM quotation WHERE item=ITEM 
IF (item_id > 0) 
    THEN INSERT INTO.... 
    ELSE RETURN FALSE 
END IF; 
END| 

這是一個公正的例子,更多信息查詢

http://www.databasejournal.com/features/mysql/article.php/3547931/MySQL-Stored-Procedures-Part-2.htm

+0

我在過去執行程序時遇到問題..我無法解決問題。有程序的替代方法嗎? – chupinette 2010-04-04 16:26:33

+0

有替代品,但最好的辦法是用功能解決它... – confiq 2010-04-04 16:48:10

+0

你能告訴我什麼是替代品? – chupinette 2010-04-04 16:49:29