[英]How to mix SQL data with OR condition
I have a SQL query to return 2 post_type between 1st January and 31th December : 我有一个SQL查询在1月1日至12月31日之间返回2 post_type:
$url = current date (2017 for example)
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'"
My query works but the result reutnr car
first and secondly motorbike
. 我的查询有效,但结果首先是reutnr
car
,其次是motorbike
。
I would like to mix data by post_date
and not by post_type
first. 我想
post_date
而不是按post_type
混合数据。
Any idea ? 任何想法 ?
EDIT : 编辑:
Current result : 当前结果:
Car | January
Car | February
Car | April
[...]
Car | July
Car | August
Motorbike | January
Motorbike | March
Motorbike | May
Motorbike | June
[...]
What i want : 我想要的是 :
Car | January
Motorbike | January
Car | February
Motorbike | March
Car | April
Motorbike | May
Motorbike | June
Motorbike | July
Car | August
[...]
Examples with 'month' but format date is 2017-12-31 带有“ month”但格式日期为2017-12-31的示例
DEMO: http://rextester.com/MBRS91171 演示: http : //rextester.com/MBRS91171
Simplify query with an IN vs or 使用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;
Or just make sure order by is syntax correct. 或者只是确保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`
make sure post_date is correct in order by post_date
vs 'post-date' 确保post_date与
post_date
和'post-date'顺序正确
It is not clear what you are trying to do. 目前尚不清楚您要做什么。 But if you want to order by post type that are
car
first, then you can use CASE
expression in order by like this: 但是,如果要先按
car
的邮政类型进行订购,则可以按如下方式按顺序使用CASE
表达式:
...
ORDER BY CASE WHEN post_type = 'car' THEN 0 ELSE 1 END, 'post-date'
这个怎么样
ORDER BY post_date asc,post_type asc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.