Fetch Latest Entry In table. Last line in database table - mysql

I have a table
tablea
id value
__ _____
1 a
2 b
3 c
4 d
5 e
i want to fetch only latest entry in table (i.e) last value
=>id,value as 5,e
could you please tell me query for this.

use following select query
SELECT * FROM tablea where ID IN (SELECT MAX(ID) FROM tablea);

Use order by and limit:
select t.*
from t
order by id desc
limit 1;

Select id, value
from tablea
order by id desc
limit 1

Related

SELECT value BEFORE max mysql

so, i have this table generated annually :
+----+------+
| id | name |
+----+------+
| 1 | 20162|
| 2 | 20162|
| 3 | 20171|
| 4 | 20171|<<<||| "how do i get this bfore max value"
| 5 | 20172|
| 6 | 20172|
+----+------+
If i query :
SELECT name FROM table WHERE where name=(SELECT max(name))
The result is 20172
How do i get the value before that (20171)?
This will get second max.
SELECT *
FROM table
WHERE name NOT IN (SELECT MAX(name) FROM table )
ORDER BY id DESC LIMIT 1
You can do the little trick:
SELECT name FROM table ORDER BY name DESC LIMIT 1,1
If you want a portable solution (since the above will only work in MySQL probably):
SELECT name FROM table t1 where
1 = (SELECT count(distinct name)
from table t2
where t2.name > t1.name )
This selects the name which has exactly one other name which is greater than it. It will have issues when e.g. all values are the same but that may actually be the intention sometimes.
Also you can try this:
SELECT name
FROM (SELECT DISTINCT name FROM yourtable) TMP
ORDER BY name DESC LIMIT 1,1
how about this:
select b.second_id,b.name from
(select id, max(name) as max from tbl_max) as a
LEFT JOIN
(select id as second_id, name from tbl_max ) as b
on a.id <> second_id where name < a.max order by name desc limit 1;
Another solution much better:
set #max= 0;
select max(name) into #max from tbl_max;
select * from tbl_max where name < #max ORDER BY id desc limit 1
it return a result of
4 20171
If you want Max(name)
SELECT id,MAX(name) FROM table
WHERE name < (SELECT MAX(name) FROM table )
or
SELECT id,MAX(name) FROM table
WHERE name <> (SELECT MAX(name) FROM table )
And there is an article for Find nth high value
(as Question changed later, this tricks is not useful for this problem)
can you use this simple order by ?
SELECT * FROM tablename
group by name
order by name desc
limit 1,1
if you have multiple equal values in MAX(name) you need group by, even this query is not general solution!
You can try below code.It will give you nth highest record.
SELECT NAME FROM table ORDER BY name DESC LIMIT n,1
Hope this will helps
SELECT DISTINCT name FROM table ORDER BY name DESC LIMIT 1,1;
The LIMIT clause accepts two values, an offset and a count. When only 1 value is provided, it assumes the offset is 0
DISTINCT is necessary as you have duplicate values for name. If this was unintentional, it's not needed.
Example at http://sqlfiddle.com/#!9/ea6e2/2

how to find next max(id) in a table

lets say
My table contains
Id Status
1 0
2 1
3 0
4 0
5 1
6 0
I need output like
Id Status
5 1
I tried like Max(id) but it gives output as
id status
6 0
I can only suppose, that you want to know about the maximum id of those entries having Status=1, right? Then use
select max(id) from mytable where Status=1
Try this:
SELECT id, status
FROM myTable ORDER BY `id` DESC LIMIT 1 , 1
I have assumed that you are looking for the second highest Id record values.
I guess you would like to have the max(id) where Status=1?
Then use select max(id) from table where Status=1
Since you want to get the absolute max from both columns use :
SELECT GREATEST(MAX(id), MAX(status));
Your question is a bit unclear what it is you actually want. From your example, I guess you want the maximum ID for the max status.
This is the max id for every status:
select max(id), status from table
group by status;
This will result in 5,1 and 6,0.
You could then filter out what you don't need, e.g.
select * from (
select max(id), status from t
group by status
) maxidperstatus
where maxidperstatus.status = (select max(status) from t);
Try this also
select Id,Status
from table
where Id=(
select MAX(Id) from table where Id <> (select MAX(Id) from table)
)
I assume that you want the Id where the status is the biggest
Try this
select max(status),id from table group by id order by max(status) DESC

How to select the record which has the max id in mysql?

Here's the test_table in mysql:
id | C
1 c1
2 c1
3 c2
If I use:
select * from test_table
I'll got the records whose id equals 1,2,3
But what I intend to do is to retrieve records with id equals 2 and 3. That is, When field C is the same, retrieve the one who's got the max id.
Could anyone give me some idea? Thanks a lot!
You could use GROUP BY and MAX() aggregate function like:
SELECT C, MAX(id) as MaxID
FROM tableName
GROUP BY C
See Fiddle Demo
SELECT MAX(id)
FROM test_table
GROUP BY c

find second max in a table using mysql query

I have a database with one table called "user" having two fields:
"id" (type: INTEGER, PRIMARY KEY)
"name" (type: VARCHAR(32))
I want to Write a standard SQL query which retrieves the second highest value of "id" from the "user" table. The value returned should be represented using the column name "id".
I have tried this but it gives me all ids:
SELECT `user`.`id`
FROM `user`
ORDER BY `user`.`id` ASC
LIMIT 0 , 30
some example data in my table:
id name
----------------
1 john
2 david
3 mike
I want to get '2' but now i'm getting :
id
----
1
2
3
I can do it with help of PHP but I want to know the way with mysql (SQL).
thanks
SELECT id
FROM `user`
ORDER BY id DESC -- start with highest
LIMIT 1 -- show only 1 row
OFFSET 1 ; -- but skip the first (skip 1 row)
SELECT id FROM user ORDER BY id ASC LIMIT 1, 1
select max(id) from user where id < (select max(id) from user);
select max(id) from user where id not in (Select max(id) from user);
SELECT id
FROM `user`
ORDER BY id ASC
LIMIT 1, 1
Using limit you can set an offset and the number of records you like being returned.
How 'bout
Select max(id) from user
where id <> (select max(id) from user)
This may work:
select max(id)-1 from user;

Order mysql query in the same order I provide the OR statements in

Here's a query:
SELECT *
FROM table
WHERE id = 1
OR id = 100
OR id = 50
Note that I provided the ids in this order: 1,100,50.
I want the rows to come back in that order: 1,100,50.
Currently, i comes back 1,50,100 - basically in ascending order. Assume the rows in the table were inserted in ascending order also.
Use the MySQL specific FIND_IN_SET function:
SELECT t.*
FROM table t
WHERE t.id IN (1, 100, 50)
ORDER BY FIND_IN_SET(CAST(t.id AS VARCHAR(8)), '1,100,50')
Another way to approach this would put the list in a subquery:
select table.*
from table join
(select 1 as id, 1 as ordering union all
select 100 as id, 2 as ordering union all
select 50 as id, 3 as ordering
) list
on table.id = list.id
order by list.ordering
You can just do this with ORDER BY:
ORDER BY
id = 1 DESC, id = 100 DESC, id = 50 DESC
0 is before 1 in ORDER BY.
Try this
SELECT *
FROM new
WHERE ID =1
OR ID =100
OR ID =50
ORDER BY ID=1 DESC,ID=100 DESC,ID=50 DESC ;
http://www.sqlfiddle.com/#!2/796e2/5
... WHERE id IN (x,y,x) ORDER BY FIELD (id,x,y,z)