I have a MySQL table where I would like to get the latest posts by "post_date" from a set of users in a particular group (they can belong to more than one group). I first get all users in the group. Then I try:
SELECT *, max(post_date) FROM posts
WHERE user_id IN ($matches)
GROUP BY user_id
ORDER BY post_date DESC
But this doesn't work. How do I solve it?
To get the latest posts from group of users, you can do so by using a self join
SELECT p.*
FROM posts p
JOIN(
SELECT user_id , max(post_date) post_date
FROM posts
GROUP BY user_id
) pp
USING(user_id,post_date)
WHERE pp.user_id IN ($matches) /* i assume here you have user ids like IN (1,2,3)*/
ORDER BY p.post_date DESC
try that:
"SELECT *, max(post_date) FROM posts
WHERE user_id IN (".implode(',',$matches).")
GROUP BY user_id
ORDER BY post_date DESC "
Related
I have a Wordpress widget and it lists top authors according to post count.
I want it to sort as last activity (last post). But i can't.
My code is:
"SELECT users.ID, COUNT( posts.ID ) AS post_count
FROM {$wpdb->users} AS users
RIGHT JOIN {$wpdb->posts} AS posts ON posts.post_author = users.ID
WHERE posts.post_type='post' AND posts.post_status='publish'
GROUP BY users.ID
ORDER BY post_count DESC
LIMIT %d"`
Output:
How can i sort the authors according to last post?
Try this i assume created column is exist in posts table
"SELECT users.ID, posts.ID
FROM {$wpdb->posts} AS posts
JOIN {$wpdb->users} AS users ON posts.post_author = users.ID
WHERE posts.post_type='post' AND posts.post_status='publish'
GROUP BY users.ID
ORDER BY posts.created DESC
LIMIT %d"`
I have "user_likes" table with id(int(10) key), user_id(varchar(15)), post_id(varchar(15)) and "user_post" table with post_id(int(15) key), user_id(varchar(15)), post_txt(text)
I now order the posts by id:
$que_posts = mysql_query("select * from user_post order by user_id DESC");
I want to order it by amount of likes to each post(i.e. posts with more likes will be first).
The problem is that the LIKES are in different table. How can I do it?
Edit- I using sef4eg's answer(with changes) I could fix it:
"SELECT user_post.*, COUNT(user_likes.post_id) AS like_count
FROM user_post LEFT JOIN user_likes
ON user_post.post_id = user_likes.post_id
GROUP BY user_post.post_id
ORDER BY like_count desc;";
Now you have like_count in your select so you display it if you need to
SELECT user_post.*, COUNT(user_likes.id) as like_count
FROM user_post
LEFT JOIN user_likes
ON user_likes.user_id = user_post.user_id AND user_likes.post_id = user_post.post_id
GROUP BY user_post.post_id
ORDER BY like_count DESC
Hi I am having a problem with following query.
SELECT id, user_id, cloth_id FROM `items` GROUP BY user_id ORDER BY id desc LIMIT 3
I want the latest records with group by but somehow its showing old records.
I have also gone through MySQL - Group by with Order by DESC but not working as expected.
Try this:
SELECT i.id, i.user_id, i.cloth_id FROM
(
SELECT max(id) as id, user_id FROM `items` GROUP BY user_id
) temp
LEFT JOIN `items` i on i.user_id = temp.user_id AND i.id = temp.id
in temp you get each user with the latest id.
in i you get the cloth_id for that combination
I've read many answers to this question but none seem to answer in the scenario when you need to pick one random row in a DB of articles, say a wordpress db, where in the wp_posts, you actually have both revisions, trashed and published articles.
Previous answers seems to return blank result if the id is random and the post is not published like in this code
SELECT * FROM wp_posts AS w
JOIN (SELECT (RAND() * (SELECT MAX(id) FROM wp_posts)) AS id) AS r2
WHERE w.id >= r2.id
AND w.post_status = 'publish'
ORDER BY w.id ASC
LIMIT 1
Just use
SELECT *
FROM wp_posts
WHERE post_status = 'publish'
ORDER BY RAND()
LIMIT 1
to get a random record being published.
Your method is intended to be more efficient than sorting all the published articles. The following is similar for only published articles:
SELECT *
FROM wp_postsw CROSS JOIN
(SELECT (RAND() * (SELECT MAX(id) FROM wp_posts WHERE post_status = 'publish')) AS id
) r2
WHERE w.id >= r2.id AND w.post_status = 'publish'
ORDER BY w.id ASC
LIMIT 1;
For performance, you want an index on wp_posts(post_status, id).
That is, you need the condition inside the subquery. And, you need to include the appropriate columns in the WHERE clause.
I have 2 tables(I'm making kind of forum) I would like to sort topics order by last post that has been written(order by row 'posts.date_written'). I was trying to do it by myself but I couldn't get it working.
Topic table:
ID|id_subforum|owner_id|owner|title|description
Posts table:
ID|id_subforum|id_topic|by|description|date_written
Can somebody show me how should this SQL look like?
SELECT t.*
FROM topics AS t
LEFT JOIN (
SELECT id_topic, id_subforum, MAX(date_written) AS last_date
FROM posts
GROUP BY id_topic, id_subforum
) AS p
ON t.id = p.id_topic AND t.id_subforum = p.id_subforum
WHERE t.id_subforum = '$forumId'
ORDER BY last_date DESC