如何用SQL语句查询最近(正在进行或即将开始)的团购列表信息?

霞舞
发布: 2025-02-23 10:58:18
原创
399人浏览过

如何使用 sql 查询最近团购列表信息

对于你的问题,我们需要明确以下两点:

  1. 如果存在正在进行的团购,则查询正在进行的团购。
  2. 如果不存在正在进行的团购,则查询离当前时间最近、即将开始的团购(而不是所有未开始的团购)。

sql 查询语句:

select
    t4.*,
    now() as 当前时间
from
(
    select
        substring_index(
            substring_index(a.team_id_list, ',', b.help_topic_id + 1),
            ',',
            -1
        ) as team_id
    from
    (
        select
            team_id_list
        from
        (
            select
                group_concat(team_id) as team_id_list
            from
                team_found
            where
                team_start_time <= now() and team_end_time > now()
            union all
            select
                team_id_list
            from
            (
                select
                    group_concat(team_id) as team_id_list
                from
                    team_found
                where
                    team_start_time > now()
                group by
                    team_start_time,
                    team_end_time
                limit 1
            ) t0
        ) t1
        where
            t1.team_id_list is not null
        limit 1
    ) t2
    join mysql.help_topic b on b.help_topic_id < (
        length(a.team_id_list) - length(replace(a.team_id_list, ',', '')) + 1
    )
) t3
left join team_found t4 on t3.team_id = t4.team_id;
登录后复制

查询结果示例:

现在时间为2021-3-23 10:21:30

酷表ChatExcel
酷表ChatExcel

北大团队开发的通过聊天来操作Excel表格的AI工具

酷表ChatExcel 48
查看详情 酷表ChatExcel
+--------+--------+---------------------+---------------------+----------+------------+
| team_id | prod_id | team_start_time      | team_end_time        | prod_name | 当前时间  |
+--------+--------+---------------------+---------------------+----------+------------+
|      2 |      1 | 2021-03-23 11:00:00 | 2021-03-23 11:30:00 | 猪       | 2021-03... |
+--------+--------+---------------------+---------------------+----------+------------+
登录后复制

现在时间为2021-3-23 12:05:05

+--------+--------+---------------------+---------------------+----------+------------+
| team_id | prod_id | team_start_time      | team_end_time        | prod_name | 当前时间  |
+--------+--------+---------------------+---------------------+----------+------------+
|      3 |      2 | 2021-03-23 12:00:00 | 2021-03-23 12:30:00 | 狗       | 2021-03... |
+--------+--------+---------------------+---------------------+----------+------------+
登录后复制

现在时间为2021-3-23 21:31:00

+--------+--------+---------------------+---------------------+----------+------------+
| team_id | prod_id | team_start_time      | team_end_time        | prod_name | 当前时间  |
+--------+--------+---------------------+---------------------+----------+------------+
|     15 |      8 | 2021-03-24 11:00:00 | 2021-03-24 11:30:00 | 兔       | 2021-03... |
|     16 |      8 | 2021-03-24 11:00:00 | 2021-03-24 11:30:00 | 兔       | 2021-03... |
+--------+--------+---------------------+---------------------+----------+------------+
登录后复制

以上就是如何用SQL语句查询最近(正在进行或即将开始)的团购列表信息?的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号