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
Related
Im new to msql! Can any one re-correct me
My table name = details, column = id, name, mobile, email, city, state, Pincode, difftime
I need to retrieve data unique by ID,
get count on Id retrieve on desc order
difftime(Datetime) in Asc order
Please help me on this,
select * from details
where id s
join (SELECT id FROM `details`
group by id
ORDER BY `difftime` ASC,count(id) desc) d
on d.id=s.id
ORDER BY s.`difftime`
LIMIT 5
select D2.*, D1.* from details D1
left outer join (SELECT D.id, count(*) as total_ids FROM `details` D
group by D.id
) D2
on D1.id=D2.id
ORDER BY D1.`difftime` ASC, D2.total_ids desc
LIMIT 5
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 "
well, i'm new to MySQL Database and i got a problem, i need to get the last two set of records from a table ( the records are automatically added to this table every week) i need to use to find the growth that an entity did during the last week.
can any one help plz.
here is what i wrote, i tested it on my local host and it worked :D, but when we installed it online, it crashed :(
select pages.*, new.* from (
select id, tableone.page_id, one, two,(two - one) as diff from
(SELECT id, page_id, likes as two FROM `page_records` WHERE id IN ( SELECT max( id ) FROM `page_records` GROUP BY page_id )) as tableone left join
(SELECT page_id , likes as one FROM `page_records` where id in ( SELECT max(id) FROM `page_records` where id not in (select max(id) from `page_records` group by page_id) group by page_id))
as tabletwo
on tableone.page_id = tabletwo.page_id
order by tableone.page_id asc) as new inner join pages on pages.id = new.page_id
Thanks in advance.
try:
SELECT id, page_id, likes FROM page_records order by id desc limit 0, 2
Try this:
SELECT * FROM table_name
ORDER BY id DESC
LIMIT 0, 2
Thanks!
I have 3 tables - News, Content and Edits.
Every row in each table contains a user_id field.
What I want to do is make a list of the top contributors to these 3 tables.
So I need to count how many times each different user_id appears in each table and order from the highest count to the lowest.
Like this:
SELECT
user_id,
COUNT(*) AS all_actions
FROM (
SELECT user_id FROM News
UNION ALL
SELECT user_id FROM Content
UNION ALL
SELECT user_id FROM Edits
) tmp_table
GROUP BY user_id
ORDER BY all_actions DESC
This should do it.
SELECT all_user_ids.user_id as `user_id`, COUNT(all_user_ids.user_id) as `count`
FROM
(SELECT user_id FROM news
UNION ALL
SELECT user_id FROM content
UNION ALL
SELECT user_id FROM edits) AS all_user_ids
GROUP BY `user_id`
ORDER BY `count` DESC
This would be a per table instance
SELECT DISTINCT(COUNT(user_id)) as num FROM news GROUP BY user_id ORDER BY num DESC LIMIT 0,3
I currently have this
SELECT *
FROM images
WHERE images.id IN (SELECT image_id FROM image_likes WHERE user_id = '1')
UNION
SELECT *
FROM images
WHERE images.user_id = '1' AND upload_type in (4,3) ORDER BY id DESC
And this works great, but now i want it to display the most recent results thats have happend.
For example, User A uploaded a image-> information goes into images with a timestamp. User A then likes someone else's image -> Information goes into image_likes with a timestamp. Now how would i make the two timestamp column's merge together so i can sort them by DESC.
Breif run over of what the query does.
Selects the image information from images where the user has liked a image in image_likes and then it grabs all of the uploads the user has uploaded from images and merges them into a query. What i need is to be able to sort them using the timestamp from both tables.
how about doing this??
SELECT * FROM (
SELECT *
FROM images
WHERE images.id IN (SELECT image_id FROM image_likes WHERE user_id = '1')
UNION
SELECT *
FROM images
WHERE images.user_id = '1' AND upload_type in (4,3) ORDER BY id DESC
) AS a ORDER BY a.timestamp DESC;
OR even better
(SELECT *
FROM images
WHERE images.id IN (SELECT image_id FROM image_likes WHERE user_id = '1'))
UNION
(SELECT *
FROM images
WHERE images.user_id = '1' AND upload_type in (4,3) ORDER BY id DESC)
ORDER BY timestamp DESC
chk this http://dev.mysql.com/doc/refman/5.5/en/union.html
UPDATE
TRY THIS
(SELECT images.id, images_likes.timestamp as timestamp FROM images JOIN images_likes
ON images.id=image_likes.image_id WHERE user_id = '1')
UNION
(SELECT images.id, images.timestamp as timestamp
FROM images
WHERE images.user_id = '1' AND upload_type in (4,3) ORDER BY id DESC)
ORDER BY timestamp DESC
UPDATE
Final query as per your requirement
(SELECT images.id, images.user_id, images.ext, images.upload_type, images_likes.timestamp as timestamp FROM images JOIN images_likes
ON images.id=image_likes.image_id WHERE images_likes.user_id = '1')
UNION
(SELECT images.id, images.user_id, images.ext, images.upload_type, images.timestamp as timestamp
FROM images
WHERE images.user_id = '1' AND upload_type in (4,3) ORDER BY id DESC)
ORDER BY timestamp DESC
I would approach with the following... The "PreQuery" below starts with all images the user likes first, with the max timestamp as a new column "MostRecent". THEN, do a UNION ALL for the next set. The next set is directly from the images table for the user, but LEFT JOINED to the "Liked" images. Then the WHERE clause explicitly excludes those that would have been returned in the first set by using the "iml2.image_id IS NULL". I did this as I wasn't positive on how the union would handle the group by in the first set, but no group by in the second. Ex: If the image was created with date/timestamp of say Mar 3, but also found as a "Like" of Mar 5, I didn't want one instance listed as each distinct date (as union already qualifies distinct implied).
That said, I will have a single prequery result of any image the person liked with its most recent date/time stamp, AND all their own personal images with their respective date/time stamp. Now, its a simple join back to the images table to grab the details (via alias "i2") and we can sort by the PreQuery.MostRecent column, then by the image ID value descending.
SELECT
i2.*
from
( select iml.image_id as id,
max( iml.YourTimeStamp ) as MostRecent
from image_likes iml
where iml.user_id = '1'
group by iml.image_id
UNION ALL
select i.id,
i.ImageTimeStamp as MostRecent
from images i
left join image_likes iml2
on i.id = iml2.image_id
and iml2.user_id = '1'
where i.user_id = '1'
and i.upload_type in ( 4, 3 )
and iml2.image_id is null
) PreQuery
JOIN Images i2
on PreQuery.id = i2.id
order by
PreQuery.MostRecent,
PreQuery.ID desc
Why don't you use a 'OR'?
that way :
SELECT *
FROM images
WHERE images.id IN (SELECT image_id FROM image_likes WHERE user_id = '1')
OR (images.user_id = '1' AND upload_type in (4,3))
ORDER BY id DESC
SELECT *
FROM
( SELECT im.*
, li.timestamp AS ts
FROM images AS im
JOIN image_likes AS li
ON li.image_id = im.image_id
WHERE li.user_id = 1
UNION ALL
SELECT *
, timestamp AS ts
FROM images
WHERE user_id = 1
AND upload_type in (4,3)
) AS tmp
ORDER BY ts DESC