MySQL在where子句中进行条件查询
我不确定我是否遗漏了一些非常明显的东西,但我在这个查询上不断收到语法错误。 即使我错过了一些明显的东西,我也想知道是否有更聪明的方法来获得我想要的东西。
基本上,查询要求提供与 start_date 介于星期一和星期五之间的用户相关的任何行。 效果很好。 但后来我添加了一个条件查询,以防周六或周日有任何行。 请注意,条件查询正在检查周六或周日的任何用户,而不是主查询中的用户:
SELECT user_id,
DATE_FORMAT(DATE(shift_start),'%m/%d/%Y') date,
TIME_FORMAT(TIME(shift_start), '%h:%i %p') start,
TIME_FORMAT(TIME(shift_end), '%h:%i %p') end,
title
FROM shifts
WHERE user_id = '$user_id'
AND DATE(shift_start) BETWEEN
DATE_SUB(DATE(NOW()), INTERVAL WEEKDAY(NOW()) DAY) AND
DATE_ADD(DATE(NOW()), INTERVAL
(SELECT
IF(
COUNT(*) FROM shifts
WHERE DATE(shift_start) BETWEEN
DATE_ADD(DATE(NOW()), INTERVAL 5 - WEEKDAY(NOW()) DAY) AND
DATE_ADD(DATE(NOW()), INTERVAL 6 - WEEKDAY(NOW()) DAY),
6, 4)) - WEEKDAY(NOW()) DAY)
ORDER BY shift_start
实际上,我对它在与 IF 部分混淆之前的工作方式感到非常自豪,但同样,如果有显然这是更好的方法,我洗耳恭听。
哦,当这个问题得到解决时,“Now()”将被替换为 php 脚本中设置的日期变量(通过 GET 传递给它)。
干得好,本卢米。 这是有效的:
SELECT user_id,
DATE_FORMAT(DATE(shift_start),'%m/%d/%Y') AS shift_start_date,
TIME_FORMAT(TIME(shift_start), '%h:%i %p') AS shift_start_time,
TIME_FORMAT(TIME(shift_end), '%h:%i %p') AS shift_end_time,
title
FROM shifts
WHERE user_id = '$user_id' AND
DATE(shift_start) BETWEEN
DATE_SUB(DATE(NOW()), INTERVAL WEEKDAY(NOW()) DAY)
AND
DATE_ADD
(
DATE(NOW()), INTERVAL
(
SELECT IF(COUNT(*),6,4)
FROM shifts
WHERE DATE(shift_start) BETWEEN
DATE_ADD(DATE(NOW()), INTERVAL 5 - WEEKDAY(NOW()) DAY)
AND
DATE_ADD(DATE(NOW()), INTERVAL 6 - WEEKDAY(NOW()) DAY)
) - WEEKDAY(NOW()) DAY
)
I'm not sure if I'm missing something really obvious, but I keep getting a syntax error on this query. Even if I AM missing something obvious, I'd like to know if there is a smarter way of getting what I'm after.
Basically, the query asks for any rows tied to a user with a start_date between Monday and Friday. That works great. But then I added a conditional query in case there are any rows for that Saturday or Sunday. Note that the conditional query is checking for ANY users with a Saturday or Sunday, not the user in the main query:
SELECT user_id,
DATE_FORMAT(DATE(shift_start),'%m/%d/%Y') date,
TIME_FORMAT(TIME(shift_start), '%h:%i %p') start,
TIME_FORMAT(TIME(shift_end), '%h:%i %p') end,
title
FROM shifts
WHERE user_id = '$user_id'
AND DATE(shift_start) BETWEEN
DATE_SUB(DATE(NOW()), INTERVAL WEEKDAY(NOW()) DAY) AND
DATE_ADD(DATE(NOW()), INTERVAL
(SELECT
IF(
COUNT(*) FROM shifts
WHERE DATE(shift_start) BETWEEN
DATE_ADD(DATE(NOW()), INTERVAL 5 - WEEKDAY(NOW()) DAY) AND
DATE_ADD(DATE(NOW()), INTERVAL 6 - WEEKDAY(NOW()) DAY),
6, 4)) - WEEKDAY(NOW()) DAY)
ORDER BY shift_start
I'm actually pretty proud of how it works before it messes up with the IF part, but again, if there is an obviously better way doing this, I'm all ears.
Oh, and when this gets ironed out, the "Now()" will be replaced with a date variable set up in the php script (passed to it via GET).
Awesome job, benlumey. Here's what worked:
SELECT user_id,
DATE_FORMAT(DATE(shift_start),'%m/%d/%Y') AS shift_start_date,
TIME_FORMAT(TIME(shift_start), '%h:%i %p') AS shift_start_time,
TIME_FORMAT(TIME(shift_end), '%h:%i %p') AS shift_end_time,
title
FROM shifts
WHERE user_id = '$user_id' AND
DATE(shift_start) BETWEEN
DATE_SUB(DATE(NOW()), INTERVAL WEEKDAY(NOW()) DAY)
AND
DATE_ADD
(
DATE(NOW()), INTERVAL
(
SELECT IF(COUNT(*),6,4)
FROM shifts
WHERE DATE(shift_start) BETWEEN
DATE_ADD(DATE(NOW()), INTERVAL 5 - WEEKDAY(NOW()) DAY)
AND
DATE_ADD(DATE(NOW()), INTERVAL 6 - WEEKDAY(NOW()) DAY)
) - WEEKDAY(NOW()) DAY
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这个子查询:
我所做的两件事
Try this subquery:
Two things I've done
这在这里崩溃了:
count 语句不起作用。
你想做什么? 您需要清楚地说明您要在这里完成的任务。
这是我的风格:
但如果不知道你想做什么,我不确定。
this falls apart around here:
The count statement won't work.
What are you trying to do? You need a clear statement of what you are trying to accomplish here.
This is my swag at it:
But without knowing what you are trying to do I'm not sure.