I have a table like this in MYSQL:
SELECT * FROM test_table
id u_id name date
1 101 name2 2012-05-14
2 305 name3 2012-05-11
3 506 name4 2012-05-05
4 207 name5 2012-05-12
5 108 name6 2012-05-03
SELECT id,u_id from test_table order by date;
id u_id
5 108
3 506
2 305
4 207
1 101
I have a application where where these things are displayed
on clicking on any u_id it takes me to a different page which displyed details stuff
Now I want to write a query which willl give me the next record
How do I achive the next record from here like
when I say u_id>305 it shoud give me 207?
and u_id<305 it should give me 506
I think this is what you are looking for:
SELECT id, u_id
FROM test_table
WHERE uid > 305
ORDER BY date ASC
LIMIT 1;
SELECT id, u_id
FROM test_table
WHERE uid < 305
ORDER BY date DESC
LIMIT 1;
Given the u_id is 305, to get the next record by date:
SELECT id,u_id
FROM test_table a
WHERE a.date > (SELECT date FROM test_table b WHERE b.u_id = 305)
ORDER BY a.date
LIMIT 1;
And to get the previous record by date:
SELECT id,u_id
FROM test_table a
WHERE a.date < (SELECT date FROM test_table b WHERE b.u_id = 305)
ORDER BY a.date DESC
LIMIT 1;
try this:
select id,u_id from
(SELECT id,u_id,#rownum:= #rownum+1 AS Sno
from test_table , (SELECT #rownum:=0) r;
order by date)a
where Sno=<#ID-1/+1>
lets say if your current Sno=5 then 4 will give the previous record and 6 will give the next record
Related
My table structure as follows
id parent last_date sub_type
----------------------------------
11 9 2017-02-28 1101
10 9 2016-08-26 1101
8 1 2017-02-20 1101
12 12 2016-08-31 1102
14 12 2016-12-20 1102
9 9 2016-12-31 1101
13 12 2017-03-23 1102
2 1 2017-01-25 1101
1 1 2016-12-31 1101
i want to fetch rows for each sub_type based the date (longest first) . i tried following query
SELECT * FROM mytable GROUP BY sub_type ORDER BY ISNULL(last_date) DESC, last_date DESC
and it results
id parent last_date sub_type
--------------------------------
1 1 2016-12-31 1101
12 12 2016-08-31 1102
But i expect below result .
id parent last_date sub_type
--------------------------------
13 12 2017-03-23 1102
11 9 2017-02-28 1101
Please guide me to get above result .
EDIT:
last_date may have NULL value which will max precedence over dated entries. Thatswhy i choose ISNULL DESC order.
You can use a correlated subquery in the WHERE clause with ORDER BY and LIMIT 1 to find the id of the row you are looking for.
SELECT *
FROM mytable t1
WHERE id = (
SELECT id
FROM mytable t2
WHERE t2.sub_type = t1.sub_type
ORDER BY ISNULL(last_date) DESC, last_date DESC, id DESC
LIMIT 1
)
Demo: http://rextester.com/DSPH11524
Note: sub_type should be indexed.
You can also do it by giving a row number based on the sub_type column and in the descending order of the last_date column.
Query
select t1.`id`, t1.`parent`, t1.`last_date`, t1.`sub_type` from
(
select `id`, `parent`, `last_date`, `sub_type`,
(
case `sub_type` when #A
then #R := #R + 1
else #R := 1 and #A := `sub_type` end
) as `rn`
from `your_table_name` t,
(select #R := 0, #A := '') r
order by `sub_type`, `last_date` desc
)t1
where t1.`rn` = 1;
Sql Fiddle demo
This is a typical question fetching one record in each group by some aggregation. Try this:
select
mytable.*
from mytable
join (
select max(last_date) as last_date, sub_type from mytable group by sub_type
) t1 on mytable.sub_type = t1.sub_type and mytable.last_date = t1.last_date
See this article How to select the first/least/max row per group in SQL.
and Related link at right:
Retrieving the last record in each group.
And demo in sqlfiddle.
Edit:
if there is no 2 same last date and null precedence, then try this:
select
mytable.*
from mytable
join (
select
max(last_date) as last_date,
max(isnull(last_date)) nulled,
sub_type
from mytable
group by sub_type
) t1 on mytable.sub_type = t1.sub_type
and (t1.nulled and mytable.last_date is null or (t1.nulled <> 1 and mytable.last_date = t1.last_date))
also demo in sqlfiddle.
You have written a wrong query.
Specify Where condition after where clause
Below query will give your expected result.
SELECT id,parent,max(last_Date),sub_type FROM mytable GROUP BY sub_type
I have a table with 100 000 record, I want to select only the none repeated.
In another word, if the row are duplicated did not show it at all
ID Name Reslut
1 Adam 10
2 Mark 10
3 Mark 10
result
ID Name Reslut
1 Adam 10
any ideas ?
You could join a query on the table with a query that groups by the name only returns the unique names:
SELECT *
FROM mytable t
JOIN (SELECT name
FROM mytable
GROUP BY name
HAVING COUNT(*) = 1) s ON t.name = s.name
Using the same set :
ID Name Result
1 Adam 10
2 Mark 10
3 Mark 10
4 Mark 20
I'm guessing the final solution would be:
ID Name Result
1 Adam 10
4 Mark 20
Using the above query previously suggested I modified it to take the result into consideration:
SELECT t1.*
FROM myTable t1
JOIN
(
SELECT name, result
FROM myTable
GROUP BY name, result
HAVING COUNT(*) = 1
) t2
WHERE
t1.name=t2.name and
t1.result = t2.result;
I have a MySQL table
discount_vouchers
------------------
id
email
test_id
My goal is to list all vouchers that appears more than once with a given email and a given test_id from the GROUP BY:
GROUP BY email, test_id
HAVING count(*) >1
How to get read of this group by?
Here is an example:
discount_vouchers
------------------
1 1#test.com 20
2 1#test.com 10
3 1#test.com 20
4 2#test.com 30
I would like to have as a result:
id email test_id count
1 1#test.com 20 2
2 1#test.com 10 1
3 1#test.com 20 2
4 2#test.com 30 2
Try something like the following
SELECT C2, counter from
(SELECT C2, COUNT(*) as counter FROM test.mytable
GROUP BY C2) as aggregation
WHERE counter > 1
Without using group by, you can do something like
SELECT a.* ,
(SELECT count(*) FROM discount_vouchers b
WHERE a.email = b.email AND a.test_id = b.test_id) as count
FROM discount_vouchers a
How about this?
Aggregate using a subquery, and use its results in order to enrich the actual table:
SELECT `discount_vouchers`.*, `counts`.`count`
FROM `discount_vouchers`
INNER JOIN (SELECT `email`, `test_id`, Count(*) AS 'count'
FROM `discount_vouchers`) AS `counts`
ON `discount_vouchers`.`email` = `counts`.`email`
AND `discount_vouchers`.`test_id` = `counts`.`test_id`;
I have following type of table.I want to output of last record(most recent) of particular group.Please suggest My sql query.
Id Name random number
-------------------------
1 A 1233
2 A 1778
3 A 1221
4 B 1298
5 B 1289
6 C 1267
I want a last record of group A
e.g.
ID Name Random number
----------------------
3 A 1221
select id, name, random from table where Name='A' order by id desc limit 1
Here is query :
select * from tbl where id IN (select max(id) from tbl group by name);
And here is fiddle: http://sqlfiddle.com/#!2/01d69/8
SELECT * From Table1 Where [Id] in (
SELECT Max([Id]) as [maxId] From Table1 Where [Name] = 'A')
Fiddle
I need to write a query where I need to retrieve the data from a table.
Table
===================================
ID | userID | Status | date |
===================================
1 3333 Queued xxxx
2 4444 Queued yyyy
3 5555 Finished zzzzz
5 6666 Queued iiiii
6 7777 Queued kkkkk
Now i want to retrieve the row only if the status="Queued" and rows with status= "Queued" are more than 2 and the row with most recent ID . ie i want the answer to be ID = 6
I tried with the below query
select * from t1 where status = "Queued" GROUP BY status HAVING count(status) > 2 ORDER BY ID DESC limit 1
You could use a subquery for that. The problem is that the ordering is done before the grouping. Something like this should work:
select * from t1 where ID = (select max(id) from t1 where status = "Queued" group by status having count(status) > 2)
This variant is also possible -
SELECT * FROM (
SELECT * FROM t1 WHERE status = 'Queued' ORDER BY ID DESC) t
GROUP BY status HAVING count(status) > 2;
The following query should solve it:
select * from t1
where id = (select max(id) from t1 where Status = 'Queued' group by Status having count(1)>2);