gpt4 book ai didi

mysql - 如何将 SQL 数据与 OR 条件混合

转载 作者:行者123 更新时间:2023-11-29 05:59:27 24 4
gpt4 key购买 nike

我有一个 SQL 查询要在 1 月 1 日到 12 月 31 日之间返回 2 个 post_type :

$url = 当前日期(例如 2017 年)

SELECT * 
FROM `posts`
WHERE `post_date`
BETWEEN '".$url."-01-01'
AND '".$url."-12-31'
AND post_status = 'publish'
AND post_type = 'car'
OR `post_date`
BETWEEN '".$url."-01-01'
AND '".$url."-12-31'
AND post_status = 'publish'
AND post_type = 'motorbike'
ORDER BY 'post-date'"

我的查询有效,但结果首先返回 car,然后返回 motorbike

我想先按 post_date 而不是按 post_type 混合数据。

有什么想法吗?

编辑:

当前结果:

Car | January
Car | February
Car | April
[...]
Car | July
Car | August
Motorbike | January
Motorbike | March
Motorbike | May
Motorbike | June
[...]

我想要什么:

Car | January
Motorbike | January
Car | February
Motorbike | March
Car | April
Motorbike | May
Motorbike | June
Motorbike | July
Car | August
[...]

带有“月份”但格式日期为 2017-12-31 的示例

最佳答案

演示:http://rextester.com/MBRS91171

使用 IN vs or 简化查询

SELECT * 
FROM posts
WHERE `post_date`
BETWEEN '2017-01-01'
AND '2017-12-31'
AND post_status = 'publish'
AND post_type in ('car', 'motorbike')
ORDER BY post_date desc;

或者只是确保 order by 语法正确。

SELECT * 
FROM `posts`
WHERE `post_date`
BETWEEN '".$url."-01-01'
AND '".$url."-12-31'
AND post_status = 'publish'
AND post_type = 'car'
OR `post_date`
BETWEEN '".$url."-01-01'
AND '".$url."-12-31'
AND post_status = 'publish'
AND post_type = 'motorbike'
ORDER BY `post_date`

确保 post_date 按 post_date 与 'post-date' 的顺序正确

关于mysql - 如何将 SQL 数据与 OR 条件混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46280346/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com