I have a SQL query to return 2 post_type between 1st January and 31th December :
$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
.
I would like to mix data by post_date
and not by post_type
first.
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
DEMO: http://rextester.com/MBRS91171
Simplify query with an 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.
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'
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:
...
ORDER BY CASE WHEN post_type = 'car' THEN 0 ELSE 1 END, 'post-date'
这个怎么样
ORDER BY post_date asc,post_type asc
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.