作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 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/
我是一名优秀的程序员,十分优秀!