MySQL COALESCE替代选择插入的结果集,而不是/或?

时间:2012-01-25 20:23:10

标签: mysql coalesce

所以我遇到了以下查询的问题。所以,查询只选择了comment.author_id或者stream.author_id进行插入,而我需要它来选择它满足where条件的地方所以它为流项目作者ID和与目标流ID相关的所有注释的作者ID插入通知。感谢所有帮助

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'

1 个答案:

答案 0 :(得分: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上的此部分。