gpt4 book ai didi

mysql - JOIN mysql if else 语句

转载 作者:行者123 更新时间:2023-11-29 10:49:40 27 4
gpt4 key购买 nike

这是一个奇怪的条件语句 - 我不确定它是否可能。

我有一个 MYSQL 查询:

SELECT 
cute_news.title, cute_news.id, cute_news.avatar, cute_news.category,cute_news.date, cute_fields.YouTubeTN, cute_fields.alternate_title, cute_fields.NewsSource, cute_fields.alternate_url, cute_fields.cf_id
FROM cute_news, cute_fields
WHERE
YEAR(FROM_UNIXTIME(cute_news.date)) = '2007' AND
cute_fields.cf_id = cute_news.cf_id AND
FIND_IN_SET ('10', cute_news.category)
ORDER BY cute_news.date DESC

我的问题是我想要:

IF ( cute_fields.cf_id = cute_news.cf_id )THEN
{ SELECT
cute_news.title, cute_news.id, cute_news.avatar, cute_news.category,cute_news.date, cute_fields.YouTubeTN, cute_fields.alternate_title, cute_fields.NewsSource, cute_fields.alternate_url, cute_fields.cf_id
FROM cute_news, cute_fields
WHERE
YEAR(FROM_UNIXTIME(cute_news.date)) = '2007' AND
cute_fields.cf_id = cute_news.cf_id AND
FIND_IN_SET ('10', cute_news.category)
ORDER BY cute_news.date DESC }
ELSE {
SELECT
cute_news.title, cute_news.id, cute_news.avatar, cute_news.category,cute_news.date
FROM cute_news
WHERE
YEAR(FROM_UNIXTIME(cute_news.date)) = '2007' AND
FIND_IN_SET ('10', cute_news.category)
ORDER BY cute_news.date DESC }
ORDER BY..

实际执行此操作最困难。也许这是不可能的。预先感谢 NINJAS!

最佳答案

如果我清楚地理解您的要求,您需要Left Join。如果将为您的 Else 部分提供 NULL

SELECT 
cute_news.title, cute_news.id, cute_news.avatar, cute_news.category,cute_news.date,
cute_fields.YouTubeTN, cute_fields.alternate_title, cute_fields.NewsSource, cute_fields.alternate_url, cute_fields.cf_id
FROM cute_news n
LEFT JOIN
cute_fields f
ON f.cf_id = n.cf_id
WHERE
YEAR(FROM_UNIXTIME(cute_news.date)) = '2007' AND
FIND_IN_SET ('10', cute_news.category)
ORDER BY cute_news.date DESC

关于mysql - JOIN mysql if else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43959311/

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