I have my table structure like below in desc order
sr group name status
4 class11 ghanshyam Joined
3 class11 ghanshyam Removed
2 class11 ghanshyam suspended
1 class11 ghanshyam joined
Now I want to fetch all record in group class11 whose status is Removed.
So my query will be
select * from table where group = 'class11' and status='Removed' group by name
but now ghanshyam has rejoined group after status removed and we are duplicating entries so it should not count in Removed, same for suspended account.
So how Can I fetch desired row from table
SELECT t1.* from [table] t1
INNER JOIN
( SELECT MAX(sr) AS nsr ,[group] ,[name] FROM [table] GROUP BY [group] ,[name] ) t2
ON t2.nsr = t1.sr
AND t1.status='Removed'
You should retrieve the record in all cases, like this :
select * from table where group = 'class11'
after doing that you can check the last status if its removed or joined.
Try this:
SELECT * FROM yourTable
WHERE group = 'class11' AND status ='Removed'
AND (SELECT MAX(sr) FROM yourTable WHERE group = 'class11' AND status ='Joined') < sr
GROUP BY name
Hope that this will help you :)
Rest for any clarification, please add comment.
Try this query it will get records which have maximum id for removed
SELECT * FROM tbl_name t WHERE t.sr IN
(SELECT max(t1.sr) FROM tbl_name t1 WHERE `group` = 'class11' GROUP BY t1.`name`)
AND t.status = 'Removed'
Check thus fiddle:
http://sqlfiddle.com/#!9/ef189a/1
Related
In first table album has id and second table album_details has sub_id which relates from album table id
I need to display count for separate id value.
SELECT DISTINCT B.SUB_ID, A . * , B.CONTENT_VALUE AS detail,
(SELECT COUNT( ID )
FROM album_details WHERE A.ID = B.SUB_ID ) AS count
FROM album AS A, album_details AS B
WHERE A.WEBSITE_ID = '571710720'
AND A.ID = B.SUB_ID
GROUP BY B.SUB_ID
LIMIT 0 , 30
Now count column shows 40 for all rows but need to display 'count' 6 for 'id=4', 'count' 3 for 'id=2'
SELECT count(SUB_ID),SUB_ID from album_details group by SUB_ID
GROUP BY is your weapon of choice.
SELECT
a.ID,
a.CONTENT_VALUE,
COUNT(ad.ID)
FROM albums AS a
LEFT JOIN album_details AS ad ON a.ID = ad.SUB_ID
GROUP BY a.ID
Feel free to add your WHERE before the GROUP BY.
Lets say first table is A and second table is B then query will be like this
select a.ID, count(b.SUB_ID) AS total
FROM A LEFT JOIN B ON A.ID = B.SUB_ID
Group by B.SUB_ID.
It might help you. If not then ask please.
select count(sub_id) as count1 from album_details where sub_id in(select id from album) WHERE album.WEBSITE_ID = '571710720'
AND album.ID = album_details.SUB_ID
I have a table as follow. I want to run a query to select the data from the table using php in which if date column is repeated then I want to take only last row of that date
id Date start end publish
1 04-Nov-2015 1000 1300 0
4 04-Nov-2015 2100 3500 0
5 05-Nov-2015 1500 3000 0
like for the below table, When I run the query then the result should come:
4 04-Nov-2015 2100 3500 0
5 05-Nov-2015 1500 3000 0
When I run the query
$select = mysql_query("select * from `entry` Group by `date`") or die(mysql_error());
Then It shows the first row of repeating table, What should I modify in the query that the result should show the last row of repeating colum
Select * from (Select * from entry order by date,id desc) x group by x.date
You can do this with this approach:
$select = mysql_query("select * from `entry` Group by `date`" ORDER BY id DESC LIMIT 1) or die(mysql_error());
Try inner query, I'm not sure following will work exactly as I cant test that now, but for getting result you have to use inner query. Inner query help me to get expected result in my case.
SELECT *
FROM entry p
WHERE id =
(SELECT max(id) FROM entry p2
WHERE p2.id = p.id)
GROUP BY p.date
ORDER BY p.id DESC;
This query will work for you:
create TABLE test (id INT PRIMARY KEY, tdate DATE, start INT);
SELECT t1.* FROM test as t1
LEFT JOIN test as t2
ON (t1.tdate = t2.tdate AND t1.id < t2.id)
WHERE t2.id IS NULL;
Try this query :-
select * from( select * from entry order by id DESC ) new Group by date
I have Problems with a select statement, as a little help here are the important columns:
Table1
ID NAME
TABLE 2
ID U_ID COUNTER
The ID of Table 1 Matches the U_ID of Table 2. Table 2 contains many entries for the same u_id.
What I want to do is to get the Name of the "user" (table 1) who has in sum the max. counter.
What I got since now is the join of the tables (Where clause depends on other rows which are not important for the problem).
Can anyone help me on this issue?
So what you need is an aggregate of an aggregate (max of sum of column). The easiest will be to create a view providing the sum and u_id end then select the max of it:
create view table2sums
as
select u_id, sum(counter) as total
from table2
group by u_id;
and then
select t1.name
from table1 t1, table2sums t2
where t1.id = t2.u_id
and t2.total >= all (
select total
from table2sums
)
In this special case you can also do it directly:
select t1.name
from table1 t1, table2 t2
where t1.id = t2.u_id
group by t1.name
having sum(t2.counter) >= all (
select sum(counter)
from table2
group by t2.u_id
)
NOTE: The other proposed solutions will show a better performance. My solution only selects the name (which is what you said you wanted) and works in any RDBMS.
There exist RDBMS without the LIMIT possibility.
In the end, I'd say: regard my solution as educational, the others as practical
SELECT name,
SUM(counter) as counter
FROM table1
JOIN table2
ON table1.id = table2.u_id
GROUP BY u_id
ORDER BY counter DESC
LIMIT 1
You can try this:
SELECT name, SUM(counter) as total_counter
FROM table1
JOIN table2
ON table1.id = table2.u_id
GROUP BY u_id
ORDER BY total_counter DESC
LIMIT 1
Working Demo: http://sqlfiddle.com/#!2/45419/4
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 table
**id name status date**
1 john 2 01.01.2010
2 mike 5 04.01.2010
3 john 2 06.01.2010
4 sam 1 08.01.2010
john has status 2 twice and i need to select john,mike from this table where status = 2 but i need to show latest record.
I cannot use order by i use it already for something else.
You can use order by for multiple criteria like this:
ORDER BY date desc, status desc
You need to use a correlated subquery such as this:
select *
from table t1
where t1.date = ( select max( t2.date )
from table t2
where t1.name = t2.name
and t1.status = t2.status )
The query would go much faster if you didn't need the ID field:
SELECT t.name, t.status, max(t.date) date
FROM table t
GROUP BY t.name, t.status
ORDER BY [whatever]
If you DID need id, AND the ID is guarenteed to be larger on the record with the newer date, you could just add max(t.id) id to the field list.
SELECT *
FROM table t
WHERE status = 2
AND date = (SELECT MAX(date) FROM table tmp WHERE tmp.name = t.name GROUP BY name)