gpt4 book ai didi

php - MySQL 中的 If 或 CASE 语句

转载 作者:行者123 更新时间:2023-11-29 13:06:54 25 4
gpt4 key购买 nike

我正在尝试在 mqsql 中使用 if 或 case 语句...这是我的查询

$db->query("SELECT 
keywords.keyword,
keywords.keyid,
sources.s_title,
sources.s_disc,
sources.sourceid,
sources.s_link
FROM link_ks
INNER JOIN keywords ON link_ks.keyid = keywords.keyid
INNER JOIN sources ON link_ks.sourceid = sources.sourceid
case when
thumbsup_items.votes_up+thumbsup_items.votes_down != ''
then
INNER JOIN thumbsup_items ON link_ks.sourceid = thumbsup_items.name
else null
WHERE link_ks.keyid = :keyid
order by
if((thumbsup_items.votes_up+thumbsup_items.votes_down) != '',
((thumbsup_items.votes_up+thumbsup_items.votes_down) desc ,thumbsup_items.votes_up desc,sources.sourceid),
sources.sourceid )
");

我已经使用了 CASE 并且 if Both 都是这个查询,但没有任何效果我知道 CASE 和 IF 都给出错误,但我不知道如何解决这个问题...

错误

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax

编辑 我的问题是这样的。如果没有投票,此查询不会给出任何输出,因此我使用 IF 语句

$db->query("SELECT 
keywords.keyword,
keywords.keyid,
sources.s_title,
sources.s_disc,
sources.sourceid,
sources.s_link
FROM link_ks
INNER JOIN keywords ON link_ks.keyid = keywords.keyid
INNER JOIN sources ON link_ks.sourceid = sources.sourceid
LEFT JOIN thumbsup_items ON thumbsup_items.votes_up+thumbsup_items.votes_down != '' AND link_ks.sourceid = thumbsup_items.name
WHERE link_ks.keyid = :keyid
if((thumbsup_items.votes_up+thumbsup_items.votes_down) != '',
(thumbsup_items.votes_up+thumbsup_items.votes_down) desc ,
sources.sourceid )
");

现在 If 语句出错...请帮忙

最佳答案

将大小写移动到连接条件内,尝试以下操作:

SELECT 
keywords.keyword,
keywords.keyid,
sources.s_title,
sources.s_disc,
sources.sourceid,
sources.s_link
FROM link_ks
INNER JOIN keywords ON link_ks.keyid = keywords.keyid
INNER JOIN sources ON link_ks.sourceid = sources.sourceid
LEFT JOIN thumbsup_items ON thumbsup_items.votes_up+thumbsup_items.votes_down != '' AND link_ks.sourceid = thumbsup_items.name
WHERE link_ks.keyid = :keyid
ORDER BY
CASE
WHEN (thumbsup_items.votes_up+thumbsup_items.votes_down) != ''
THEN (thumbsup_items.votes_up+thumbsup_items.votes_down)
ELSE sources.sourceid
END DESC

关于php - MySQL 中的 If 或 CASE 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22574470/

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