P
P
pLavrenov2015-01-07 19:22:53
MySQL
pLavrenov, 2015-01-07 19:22:53

How to make sql query with sorting?

you need to make a selection of posts from the table

SELECT * FROM `wp_posts` WHERE post_type = 'news' LIMIT 5

but you need to display all this by the number of "hearts" for the last month (they are stored in the wp_like_post table)
Structure wp_like_post
Nz8VYui.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2015-01-07
@Nc_Soft

SELECT COUNT(*) as c, post_id FROM wp_posts 
WHERE post_type = 'news' AND time BETWEEN '2015-01-01' AND '2015-01-31 23:59:59'
GROUP BY post_id 
ORDER BY c DESC
LIMIT 5

We received 5 Yiddish posts + the number of hearts, we get these posts with the second request and display them.

B
Boris Lapin, 2015-01-07
@MrBoriska

Click on one of the column headers in PhpMyAdmin (since you are showing a screenshot from it) and you will see a query according to which the DBMS showed you the sorted data.
but in general, the construction is responsible for this:
Where name_field is the name of the column by which to sort, and DESC or ASC control the sort order (ascending, descending)
UPD:
And for a period of time, use BETWEEN, as shown by Nc_Soft

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question