DB table structure above
Result
See the images
SELECT max(comment_date), comment_content FROM wp_comments WHERE comment_post_id = 8687
It selects the field with latest date but wrong content.
I want to implement this in the below query which already has one GROUP BY statement and multiple JOINS, using limit alters the result.
Your query should fail, because it is an aggregation query and it has no group by but has unaggregated columns.
The simple way to do this uses order by and limit:
SELECT c.*
FROM wp_comments c
WHERE comment_post_id = 8687
ORDER BY comment_date DESC
LIMIT 1;
use a Join with the same table with the max date and searching for the coincidence
SELECT comment_date, comment_content
FROM wp_comments a
JOIN (SELECT comment_post_id, max(comment_date) as maxdate FROM wp_comments) b ON
b.comment_post_id = a.comment_post_id
and b.maxdate = a.comment_date
WHERE comment_post_id = 8687
Select max date in sub query and fetch required columns.
SELECT comment_date, comment_content
FROM wp_comments
WHERE comment_post_id = 8687
AND comment_date = (Select max(b.comment_date) from
wp_comment b)
Related
I'm trying to show a list with the last posts from each user. If I group by ID however I get the first post instead of the last. How can I group by UID and show only the row with the biggest Date?
This is what I'm trying now:
SELECT * FROM Posts GROUP BY `UID` ORDER BY `Date` DESC
Because you want the largest Date per user, you can use MySQL's MAX():
SELECT MAX(`Date`), * FROM Posts GROUP BY `UID`
You can also specify it in the HAVING clause too:
SELECT *
FROM Posts
GROUP BY `UID`
HAVING `Date` = MAX(`Date`)
ORDER BY `Date` DESC
You can do this using a join, to get the max date, and then only choosing those records:
select p.*
from posts p join
(select uid, max(date) as maxdate
from posts p
group by uid
) pmax
on p.uid = pmax.uid and
p.date = pmax.maxdate
I have these columns for table comments:
id
content
add_date
uid
school_id
Rows can have the same school_id.
I want to select the latest data according to add_date, but only 1 row per school_id (no duplicate for school_id) with limit of 10.
I've tried many codes already and its not working for me.
Any help would be appreciated.
This is what we call Greatest N per Group. You can achieved this by putting into a subquery so it can be joined against the non-grouped table (comments).
Try this:
SELECT c.*
FROM
(
SELECT school_id, MAX(add_date) maxDate
FROM comments
GROUP BY school_id
) x INNER JOIN comments c
ON x.school_id = c.school_ID AND
x.maxDate = c.add_date
ORDER BY x.maxDate desc
LIMIT 10
select C.ID, C.Content, t1.MaxDate as [add_date], C.uid, t1.school_id
from (selet school_id, max(add_Date) as 'MaxDate'
from comments
group by school_id) T1
inner join comments C on T1.school_id = C.school_id and C.add_Date= T1.MaxDate
LIMIT 10
If you want to choose which 10 rows return, add an order by, or a Where clause
select c1.*
from comments c1
where add_date = (select max(add_date) from comments c2 where c2.school_id =c1.school_id)
order by add_date desc
limit 10
create indexes on comments(add_date) and comments(school_id, add_date)
I have 2 tables. MySql
users : A_ID,name
event : B_ID, A_ID,cat_id,date
Now I want to get all users , who participated more at events on a given period of time, and need to add based on category too. I am doing something like this :
select name from users ,
(select A_id, count(*)
from event
where date<=givendate
group by A_id
order by count(*) desc ) e
where users.A_id=e.a_id
limit 0,5
Is there any easy and prof way to write that script ?
Thanks
Your query looks OK apart from a few minor points:
Your ORDER BY and LIMIT should be in the outer select otherwise the order of the results is indeterminate.
Use the JOIN keyword to join two tables.
Try this:
SELECT u.name
FROM users AS u
JOIN
(
SELECT A_id, COUNT(*) AS cnt
FROM event
WHERE date <= givendate
AND cat_id = 42
GROUP BY A_id
) AS e
USING (A_id)
ORDER BY e.cnt DESC
LIMIT 5
The first select is
select user_id, count(*) as count
from users
where referrer IS NOT NULL
group by referrer
order by count DESC
Then based off the records returned by that query I need to get the date for the user who referred the users in the above query.
select user_id from users where token = IDS_FROM_LAST_QUERY
I know I could use a sub query and say where IN (subquery) but I'm getting tripped up trying to keep the count from the subquery.
So in the end I need the following info
user_id, count
select o.user_id user_id, count(*) count
from users o
join users i on o.token = i.user_id
where i.referrer is not null
group by referrer
order by count desc
I would use a CTE (common table expression). CTE is super handy to look to get one population and then query the same or slightly different population from the CTE.
WITH Referrer (user_id, count) AS
(
select user_id, count(*) as count
from users
where referrer IS NOT NULL
group by referrer
order by count DESC
)
select
users.user_id
,Referrer.count
from users
inner join Referrer.user_id = users.user_id
Is it possible to pull 2 results from a sub query in a sql statement?
I have:
"SELECT
(SELECT bid FROM auction_bids WHERE itemID=a.id ORDER BY bid DESC LIMIT 1) as topbid,
a.* FROM auction_items a ORDER BY a.date DESC LIMIT 15"
The part where it returns the topbid, i'd also like it to pull not only bid (as topbid) but also date (as topdate) as well. How can I do that? Do I need another sub query or can it pull both in one?
Dependent subquery (depending on some values outside, like a.id in your case) is not a very efficient way to find maximum values in subsets.
Instead use a subquery with GROUP BY:
SELECT b.topbid, b.topdate, a.*
FROM auction_items a
LEFT JOIN
( SELECT itemID, MAX(bid) as topbid, MAX(date) as topdate
FROM auction_bids
GROUP BY itemID ) b
ON a.id = b.itemID
ORDER BY a.date DESC
LIMIT 15