gpt4 book ai didi

php - 带有条件子选择的 MySQL 复杂插入?

转载 作者:行者123 更新时间:2023-11-30 23:34:24 24 4
gpt4 key购买 nike

所以我无法正确创建此查询。

基本上,我想在通知表中插入多行,插入的每一行都有一个不会重复的唯一收件人 ID,收件人 ID 是通过检查评论的匹配 target_id 或评论的 id 来确定的流并检查与每个评论/流关联的 author_id。此外,我希望查询排除与发件人 ID 匹配的 author_id 的行插入。

下面是一个粗略的 sql 标记,基本上是我需要的。

INSERT INTO
notification (type,
target_id,
sender_id,
recipient_id,
data,
timestamp,
is_unread)
SELECT DISTINCT 'comment',
'$id',
'$senderId',
(comment.author_id OR stream.author_id),
'$dataArray',
'$timestamp',
'1'
FROM
stream,
comment
WHERE
stream.author_id != '$senderId'
AND comment.author_id != '$senderId'
AND stream.id = '$id'
OR comment.target_id = '$id'

最佳答案

你所拥有的看起来不错。您可以尝试使用 COALESCE(stream.author_id, comment.author_id),这样如果评论 author_id 为空(例如,因为 author_id 是发件人),它将返回流的 author_id。为此,您必须对流进行外部连接评论,并且 stream.author_id != '$senderId' 条件必须是 ON 子句的一部分。

关于php - 带有条件子选择的 MySQL 复杂插入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8829485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com