使用MySQL Coalesce查询进行随机插入?

时间:2012-01-23 14:20:34

标签: php mysql coalesce

我有以下查询,它偶尔会为不符合查询WHERE条件的收件人ID创建随机通知条目。我的查询的WHERE部分会导致随机插入有什么问题?

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,
        comment 
    WHERE 
         (comment.author_id != '$senderId' AND comment.target_id = '$id')
      OR (stream.author_id != '$senderId' AND stream.id = '$id')

1 个答案:

答案 0 :(得分:2)

您的streamcomment表之间没有联接条件。您正在有效地生成笛卡尔积,因此您可能包含与stream无关的comment行,反之亦然。

我建议你使用JOIN语法并声明一个条件来关联两个表中的行。

相关问题