Order by rows of different table - php

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

Related

MySQL - GROUP BY with ORDER DESC not working

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

Mysql - get last post from category

I have this structure (tables) of forum
I want to select last post (row from forum_post table) from category.
SQL so far:
SELECT * FROM table_post
WHERE topic_id = (SELECT MAX(id) FROM table_topic WHERE category_id = {$id})
ORDER BY id ASC LIMIT 1
Question: How to modify this select to achieve my goal?
Assuming that "last" means the biggest id, I would suggest order by and limit:
select fp.*
from forum_post fp join
forum_topic ft
on fp.topic_id = ft.id
where ft.category_id = $id
order by fp.id desc
limit 1;

MySQL - Latest row by set of users

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 "

mysql Selecting from two different tables.

$sql = "select body, stamp from posts where user_id = '$userid' order by stamp desc";
NOTE: the above query works fine. What I want to do is also select username from my users table, and display the username that matches the user_id.
I have edited the above statement like so, and it doesnt work. Can someone suggest the correct query? My goal is to also be able to display usernames. in addition to simply displaying user_id.
$sql = "select body, stamp from posts AND username from users where user_id = '$userid' order by stamp desc";
My goal is to also be able to display usernames. instead of simply user_id.
You'll need to use a JOIN to bring the two tables together on the matching field, so something like:
$sql = "SELECT p.body, p.stamp, u.username FROM posts p INNER JOIN users u ON p.user_id=u.user_id WHERE p.user_id='$userid' ORDER BY p.stamp DESC";
You can use table name to select the columns. Ex:
$Query = "select table1.body, table1.stamp, users.username from posts, users where user_id = '$userid' order by stamp desc";
But, this method is not good in performance.
The best method is:
$Query = "SELECT table1.body, table1.stamp, users.username
FROM posts
INNER/LEFT/RIGHT JOIN users
ON users.user_id = '$userid' AND users.user_stamp = stamp.stamp_id
All the tables must be related.
Greetings,
$sql = "select body, stamp from posts , username from users where user_id = '$userid' order by stamp desc";
I just corrected the syntactical error in your query... that is the AND keyword should be replaced by ,.
$sql = "
select body, stamp from posts where user_id = '$userid' order by stamp desc
UNION ALL
select body, stamp from username where user_id = '$userid' order by stamp desc
";
http://dev.mysql.com/doc/refman/5.0/en/union.html
You should use aliases or table name to avoid the duplicate problem like this
tablename.column
or
alias.column
you can set the alias in your where clause :
FROM table as alias_name
$sql = "SELECT p.body,p.stamp,u.username from posts p LEFT JOIN users as u ON (p.user_id = u.user_id) WHERE user_id = '$user_id' ORDER BY p.stamp DESC";
should do it
Try this:
select body, stamp, username
from posts p JOIN users a ON p.user_id = a.user_id
WHERE p.user_id = '$userid' order by stamp desc
This should work fine:
SELECT posts.body, posts.stamp, users.username
FROM posts, users
WHERE posts.user_id = '$userid' AND posts.user_id = users.user_id
ORDER BY posts.stamp DESC
select body, stamp, username
from posts,users
where users.user_id = post.user_id
and users.user_id = '$userid'
order by stamp desc;

MySQL query order the results in GROUP BY 3 Tables Count

I'm coding a listing system and I'm trying to get the posts ORDER by number of comments and votes FROM 2 tables.
Table1 : Lists => id, title, detail
Table2 : Votes => voteid, listid
Table3 : Comments => commentid, listid
WHERE MY Current query is
$q = mysql_query("SELECT * FROM zoo_leads
LEFT JOIN Votes ON Lists.id=Votes.listid
LEFT JOIN Comments ON Lists.id=Comments.listid
GROUP BY Lists.id ORDER BY Comments.listid DESC LIMIT 10
it is showing me results perfectly but ORDER BY is Lists.id Instead of number of votes and comments
Try:
SELECT *
FROM zoo_leads
LEFT JOIN votes
ON lists.id = votes.listid
LEFT JOIN comments
ON lists.id = comments.listid
GROUP BY lists.id
ORDER BY COUNT(votes.id) DESC,
COUNT(comments.id) DESC
LIMIT 10
That is because you have ORDER BY Comments.listid in your SQL statement.

Categories