MySQL在where子句中进行条件查询

发布于 2024-07-27 05:05:27 字数 1724 浏览 3 评论 0原文

我不确定我是否遗漏了一些非常明显的东西,但我在这个查询上不断收到语法错误。 即使我错过了一些明显的东西,我也想知道是否有更聪明的方法来获得我想要的东西。

基本上,查询要求提供与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尝试这个子查询:

(SELECT 
    IF(COUNT(*) > 0, 6, 4)) - WEEKDAY(NOW()) DAY) 
    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)

我所做的两件事

  • 使它看起来像一个正常的查询,据我所知,from 和 where 不能进入 if 内部。
  • 在if里放一个条件,别以为mysql会自动把0当作假,>0当作真。

Try this subquery:

(SELECT 
    IF(COUNT(*) > 0, 6, 4)) - WEEKDAY(NOW()) DAY) 
    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)

Two things I've done

  • made it look like a normal query, the from and where can't go inside the if as far as i'm aware.
  • Put a condition in the if, don't think mysql will automatically take 0 as false and >0 as true.
暮年 2024-08-03 05:05:27

这在这里崩溃了:

    (SELECT
    IF( 
       COUNT(*) FROM shifts
    WHERE DATE(shift_start) BETWEEN

count 语句不起作用。

你想做什么? 您需要清楚地说明您要在这里完成的任务。

这是我的风格:

 (SELECT
    IF( 
      (select 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))>0,6,4)
 ) - WEEKDAY(NOW()) DAY

但如果不知道你想做什么,我不确定。

this falls apart around here:

    (SELECT
    IF( 
       COUNT(*) FROM shifts
    WHERE DATE(shift_start) BETWEEN

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:

 (SELECT
    IF( 
      (select 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))>0,6,4)
 ) - WEEKDAY(NOW()) DAY

But without knowing what you are trying to do I'm not sure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文