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)
Related
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)
I am trying to retrieve unique values from the table above (order_status_data2). I would like to get the most recent order with the following fields: id,order_id and status_id. High id field value signifies the most recent item i.e.
4 - 56 - 4
8 - 52 - 6
7 - 6 - 2
9 - 8 - 2
etc.
I have tried the following query but not getting the desired result, esp the status_id field:
select max(id) as id, order_id, status_id from order_status_data2 group by order_id
This is the result am getting:
How would i formulate the query to get the desired results?
SELECT o.id, o.order_id, o.status_id
FROM order_status_data2 o
JOIN (SELECT order_id, MAX(id) maxid
FROM order_status_data2
GROUP BY order_id) m
ON o.order_id = m.order_id AND o.id = m.maxid
SQL Fiddle
In your query, you didn't put any constraints on status_id, so it picked it from an arbitrary row in the group. Selecting max(id) doesn't make it choose status_id from the row that happens to have that value, you need a join to select a specific row for all the non-aggregated columns.
Like so:
select d.*
from order_status_data2 d
join (select max(id) mxid from order_status_data2 group by order_id) s
on d.id = s.mxid
Try this Query.This will help you
SELECT id ,orderid,statusid
FROM table_name
WHERE id IN
(
SELECT max(id) FROM table_name GROUP BY orderid
)
ORDER BY statusid
You can refer this Sql_Fiddle_link which uses your example.
This is structure of product table.
Currently have more 1 million records.
I have performance issue when I use query group by & order by.
Query:
SELECT product_name FROM vs_product GROUP BY store_id ORDER BY id DESC LIMIT 2
How to improve this query to perform faster? I indexed the store_id, ID is primary key.
SELECT x.*
FROM my_table x
JOIN (SELECT store_id, MAX(id) max_id FROM my_table GROUP BY store_id) y
ON y.store_id = x.store_id
AND y.max_id = x.id
ORDER
BY store_id DESC LIMIT 2;
A hacky (but fast) solution:
SELECT product_name
FROM (
SELECT id
FROM vs_product
GROUP BY store_id DESC
LIMIT 2) as ids
JOIN vs_product USING (id);
How it works:
Your index on store_id stores (store_id, id) pairs in ascending order. GROUP BY DESC will make MySQL read the index in reverse order, that is the subquery will fetch the maximum ids for each store_id. Then you just join them back to the whole table to fetch product names.
Take notice, that the query will fetch two product names for the store ids with the maximum values.
You want a query like this:
select p.*
from product p join
(select store_id, max(id) as maxid
from product p
group by store_id
) psum
on psum.store_id = p.store_id and p.id = maxid
You don't have date in any of the tables, so I'm assuming the largest id is the most recent.
I want to return 3 rows from a table called cars and each car should have a distinct dealer_id , and I want them ordered by ID desc (so that the latest three cars added to the database are the ones returned)
SELECT id,dealer_id,name,model_year
FROM cars
GROUP by dealer_id
ORDER BY id DESC
LIMIT 3;
But this query is not returning the latest 3 cars from each distinct dealer_id
If you want the most recent records then you can use something like the following:
SELECT c1.id,
c1.dealer_id,
c1.name,
c1.model_year,
c1.date_Added
FROM cars c1
inner join
(
select dealer_id, max(date_Added) mxdate
from cars
group by dealer_id
) c2
on c1.dealer_id= c2.dealer_id
and c1.date_Added = c2.mxdate
GROUP BY dealer_id
ORDER BY id desc
LIMIT 3;
See SQL Fiddle with Demo
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