2012-01-25 41 views
0

因此,我遇到了以下查詢的問題。所以,查詢只選擇了comment.author_id或者stream.author_id進行插入,而我需要它來選擇符合where條件的地方,所以它爲流項作者ID和與目標流ID相關聯的所有評論的作者ID插入通知。所有幫助表示讚賞MySQL COALESCE替代選擇插入的兩個結果集,而不是/或?

INSERT INTO notification 
     (text, type, target_id, sender_id, recipient_id, data, timestamp, is_unread) 
SELECT DISTINCT '$text', 'comment', '$id', '$senderId', 
     COALESCE(comment.author_id, stream.author_id), 
     '$dataArray','$timestamp', '1' 
FROM stream 
    LEFT JOIN comment ON comment.target_id = stream.id 
WHERE (comment.target_id = '$id' OR stream.id = '$id') 
    AND comment.author_id != '$senderId'" 
    AND stream.author_id != '$senderId' 
+3

你不應該插入到一個字段。如果你需要兩個,有2個獨立的列 – gbn

回答

0

我同意gbn。如果由於某種原因你必須連接它們,你可以使用concat()或concat_ws()函數。

CONCAT(COL1,COL2,...):

select concat('a','b','c'); 

返回:

ABC

CONCAT_WS(分體,COL1,COL2,...):

select concat_ws(',','a','b','c'); 

退貨:

A,B,C

您需要注意與鑄造類型的特殊情況。請參閱本手冊中的String Functions