gpt4 book ai didi

WHERE 子句中的 Mysql 和 if/else 语句

转载 作者:行者123 更新时间:2023-11-28 23:41:13 25 4
gpt4 key购买 nike

我有这个 SQL 选择查询

SELECT * 
FROM bots
WHERE id NOT IN (select botid
from messages
where messages.recipient = :recipient)
limit 1

我需要做这样的事情:

SELECT * 
FROM bots
[if isset(recipient = $recipient AND sender = $sender)]
WHERE id IN (select botid
from messages
where messages.recipient = :recipient and sender = :sender)
else
WHERE id NOT IN (select botid
from messages
where messages.recipient = :recipient)
limit 1

[] - 伪代码

如何查询?

机器人表:

-------------------------------------------------------
| id | auth_token | messages_today | vkid |
-------------------------------------------------------
| 1 | token | 0 | 2323 |
-------------------------------------------------------
| 2 | token | 0 | 2343 |
-------------------------------------------------------

消息表:

-----------------------------------------------------------
| id | recipient | sender | botid | messageid |
-----------------------------------------------------------
| 1 | 12 | 1 | 1 | 3322 |
-----------------------------------------------------------
| 2 | 12 | 2 | 2 | 332123 |
-----------------------------------------------------------
| 3 | 13 | 1 | 1 | 332123 |
-----------------------------------------------------------

最佳答案

使用外连接

  • 返回机器人表中的所有机器人
  • 和匹配的消息表中的数据
  • 但随后我们消除了仅返回机器人的匹配数据对于该特定机器人不存在提供收件人和发件人的消息

返回所有机器人

Select * from bots B
LEFT JOIN messages M
on M.BotID = B.ID
and M.Sender = $sender
and M.Recipient = $Recipient
Where M.BotID is null

关于WHERE 子句中的 Mysql 和 if/else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34319408/

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