[英]How to apply IF condition with WHERE CLAUSE in this Query Using MySql
This is my MySQL query and I have a week problem in this query. 这是我的MySQL查询,我在这个查询中有一周的问题。 I don't know how to apply IF condition with
WHERE
clause. 我不知道如何使用
WHERE
子句应用IF条件。
Query: 查询:
SELECT
*,
IFNULL((SELECT ur.user_rating FROM user_rating ur
WHERE ur.vid_id = v.id AND ur.user_id = '1000'),'NULL') AS user_rating
FROM videos v
WHERE WEEK(v.video_released_date) = WEEK(NOW())
AND
v.`is_done` = 1
ORDER BY v.admin_listing ASC;
I want OR (how do I apply this condition with where clause?) 我想要OR(我如何在where子句中应用这个条件?)
IF( WEEK(v.video_released_date) = WEEK(NOW()) , WEEK(NOW()) , WEEK(NOW())-1)
=
IF( WEEK(v.video_released_date) = WEEK(NOW()) , WEEK(NOW()) , WEEK(NOW())-1)
Briefing 简报
If video released date has passed and not match with current week then previous week apply 如果视频发布日期已过,但与当前周不匹配则适用上周
Myself 我
When I was tried myself like this they return me whole data 当我像这样尝试自己时,他们会将整个数据归还给我
SELECT
*,
IFNULL((SELECT ur.user_rating FROM user_rating ur
WHERE ur.vid_id = v.id AND ur.user_id = '1000'),'NULL') AS user_rating
FROM videos v
WHERE IF(WEEK(v.video_released_date) = WEEK(NOW()),WEEK(NOW()),WEEK(NOW())-1)
= IF(WEEK(v.video_released_date) = WEEK(NOW()),WEEK(NOW()),WEEK(NOW())-1)
AND
v.`is_done` = 1
ORDER BY v.admin_listing ASC;
What am I doing wrong in this query? 我在这个查询中做错了什么?
Try this - 试试这个 -
SELECT *,
IFNULL((SELECT ur.user_rating FROM user_rating ur
WHERE ur.vid_id = v.id AND ur.user_id = '1000'),'NULL') AS user_rating
FROM videos v
WHERE WEEK(v.video_released_date) = IF(WEEK(v.video_released_date) = WEEK(NOW()),WEEK(NOW()),WEEK(NOW())-1)
AND v.is_done = 1
ORDER BY v.admin_listing ASC;
Well it looks like you compare x with x through these IF
. 好吧,看起来你通过这些
IF
比较x和x。
x = x is "always" true. x = x“始终”为真。
Use the CASE WHEN THEN ELSE END construct. 使用CASE WHEN THEN ELSE END构造。 You can read more about it here in the documentation.
你可以阅读更多关于它在这里的文档。
SELECT *,
IFNULL((SELECT ur.user_rating FROM user_rating ur
WHERE ur.vid_id = v.id AND ur.user_id = '1000'),'NULL') AS user_rating
FROM videos v
WHERE
WEEK(v.video_released_date) =
CASE WHEN WEEK(v.video_released_date) = WEEK(NOW())
THEN WEEK(NOW()) ELSE WEEK(NOW())-1 END
AND
v.`is_done` = 1
ORDER BY v.admin_listing ASC;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.