2015-11-12 34 views
0

創建功能我已經創建功能:在MySQL中1行

CREATE FUNCTION `push_admin`(`p_messenger` text, `p_admin_id` int(20), `p_client_id` int(20)) 
RETURNS INTEGER 
BEGIN 
    INSERT INTO messenger(admin_id, client_id, messenger) 
     VALUES (p_admin_id, p_client_id, p_messenger); 
    INSERT INTO messenger(ref_id) 
     SELECT id FROM messenger WHERE client_id = p_client_id; 
    RETURN LAST_INSERT_ID();  
END$$ 

我怎能捨棄admin_idclient_idmessengerref_id爲1行?

+0

你說的給是什麼意思?你問如何在mysql中調用用戶定義的函數?與您調用任何預定義函數的方式相同。 – Shadow

回答

0

在你的phpmyadmin例程中你已經添加了這個函數,你將會找到一個添加參數的選項。見下圖 -

enter image description here

0

此選項會適合你?

INSERT INTO messenger(admin_id, client_id, messenger, ref_id) 
VALUES (p_admin_id, p_client_id, p_messenger, (SELECT id FROM messenger WHERE client_id = p_client_id)); 
+0

我試過但沒有工作。 –

+0

自動遞增的ID字段? –

+0

你能幫我丹尼斯:) –

0

Referring image added在圖像 爲2行。我需要1行中的admin_id,client_id,messenger,ref_id。韓國社交協會。

0

在第二個查詢替換INSERTREPLACE

CREATE FUNCTION `push_admin`(`p_messenger` text, `p_admin_id` int(20), `p_client_id` int(20)) 
RETURNS INTEGER 
BEGIN 
    INSERT INTO messenger(admin_id, client_id, messenger) 
     VALUES (p_admin_id, p_client_id, p_messenger); 
    REPLACE INTO messenger(ref_id) 
     SELECT id FROM messenger WHERE client_id = p_client_id; 
    RETURN LAST_INSERT_ID();  
END$$ 

這將幫助你?

REPLACE與INSERT完全相同,只是如果表中的舊行與PRIMARY KEY或UNIQUE索引的新行具有相同的值,則在插入新行之前刪除舊行。

See REPLASE syntax

0

事實上,他想這樣:

CREATE FUNCTION `push_client`(`p_email` text, `p_messenger` text, `p_phone` text, `p_domain` text, `p_ip` text, `p_browser` text, `p_admin_id` int(20), `p_client_id` int(20)) 
RETURNS INTEGER 
BEGIN 
    SET @id = 0; 

    SELECT id INTO @id FROM messenger WHERE client_id = p_client_id AND admin_id = p_admin_id; 

    INSERT INTO client(ref_id, email, phone, ip, browser) 
     VALUES(@id, p_email, p_phone, p_ip, p_browser); 

    UPDATE client SET domain_id = (SELECT id FROM domain WHERE name = p_domain); 

    INSERT INTO messenger(client_id) 
     SELECT id FROM client WHERE id = p_client_id; 

    UPDATE messenger 
    SET admin_id = p_admin_id, browser = p_browser, messenger = p_messenger 
    WHERE client_id = p_client_id; 

    RETURN LAST_INSERT_ID();  
END$$