gpt4 book ai didi

mysql - 在sql语句部分使用if count

转载 作者:行者123 更新时间:2023-11-30 00:39:44 26 4
gpt4 key购买 nike

SELECT 
*
FROM
messages
WHERE
(messages.to = 'Jack' AND (type = 'message' OR type = 'reply'))
OR (messages.from = 'Jack' AND type = 'reply')
OR (messages.from = 'Jack' AND type = 'message')
ORDER BY messages.message_id DESC , messages.id DESC

我想过滤掉最后一个查询语句中的结果

(messages.from='Jack' AND type='message') 

如果它的计数等于1。

最佳答案

使用子查询,我会做这样的事情来代替最后一个条件:

messages.from = 'Jack' AND 
type = 'message' AND
1 =(select count(primary_key) from messages /* 1=count : this would ensure that
condition works only if
1 row is returned*/
where (messages.from='Jack' AND type='message') )

所以最终的 SQL 应该是:

SELECT 
*
FROM
messages
WHERE
(messages.to = 'Jack' AND (type = 'message' OR type = 'reply'))
OR (messages.from = 'Jack' AND type = 'reply')
OR (messages.from = 'Jack' AND
type = 'message' AND
1 =(select count(primary_key) from messages
where (messages.from='Jack' AND type='message') ))

ORDER BY messages.message_id DESC , messages.id DESC

关于mysql - 在sql语句部分使用if count,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21875568/

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