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 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;
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 "
$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;
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.