I need to be able to select two records from a table based on ID.
I need the first one, and the last one (so min, and max)
Example:
Customer
ID NAME
1 Bob
50 Bob
Any ideas? Thanks.
I need to be able to select two records from a table based on ID.
I need the first one, and the last one (so min, and max)
Example:
Customer
ID NAME
1 Bob
50 Bob
Any ideas? Thanks.
SELECT MIN(id), MAX(id) FROM tabla
EDIT: If you need to retrive the values of the row you can do this:
SELECT *
FROM TABLA AS a, (SELECT MIN(id) AS mini,
MAX(id) AS maxi
FROM TABLA) AS m
WHERE m.maxi = a.id
OR m.mini = a.id;
Is this what you are looking for?
select id, name from customers where id = ( select max(id) from customers )
union all
select id, name from customers where id = ( select min(id) from customers )
Now I have tested this type of query on a MySQL database I have access, and it works. My query:
SELECT nome, livello
FROM personaggi
WHERE livello = (
SELECT max( livello )
FROM personaggi )
If ties for first and/or last place are not a concern, then consider the following query:
(SELECT id, name FROM customers ORDER BY id DESC LIMIT 1)
UNION ALL
(SELECT id, name FROM customers ORDER BY id LIMIT 1);
It worked for me:
select * from customer where id in ((select min(id) from customer),(select max(id)
from customer));