作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 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/
我是一名优秀的程序员,十分优秀!