If there is a way to query according to row number ? Like:
select rownumber from table;
what i am trying to do:
I have computed the total number of rows in a table using sub queries and now I want to display only the last one.
rookie answer:
I wanted to get the 2nd highest salary. so this is what i did:
select * from gg where salary< (select max(salary) from gg) order by salary desc limit 1
This doesnot work for Nth highest salary its for 2nd highest only. AND I AM NEW TO THIS SO I JUST WANTED TO GET THE 2ND HIGHEST BY MY OWN METHOD.
Although I'm not sure why you would want to get a specific rownumer, you could do it like this:
select * from table LIMIT rownumer, 1;
Read more on this on http://dev.mysql.com/doc/refman/5.0/en/select.html
So if you want the 3rd row you'll do:
SELECT * FROM table LIMIT 3,1;
If you want the 6th-16th row you'll use:
SELECT * FROM table LIMIT 6,10
Do understand that this is only usefull to get a random row or when you add SORT BY. Otherwise you will never be sure what row will be returned...
If you want to select records with row number see this:
SELECT (#rank := #rank + 1) AS row_number
,contract_id, price1, price2, price3
FROM t1, (SELECT #rank := 0) tmp;
If you want only row number see this:
SELECT (#rank := #rank + 1) AS row_number
FROM t1, (SELECT #rank := 0) tmp
See this SQLFiddle
If you want to just limit your query to return only a specific number of rows you can use the Mysql built in LIMIT row_count OFFSET offset clause. But, If you want to select a ranking number like a row number use:
SET #rownum = 0;
Select *
FROM
(
Select *, (#rownum := #rownum+1) as rank
FROM t
) sub
Update: If you want to
I want to display only the last one.
Then you can do it this way:
SELECT *
FROM Yourquery
ORDER BY SomeField DESC --This how you determine the last one based on the order
LIMIT 1;
select * from table limit [offset,limit]
offset is the row number which you want to start,
limit is the count of rows you want to get starting from offset
so you can use it like
select * from table limit rownumber,1
There's no row number without ordering, but if you do have a sort order in mind then
SELECT * FROM thing ORDER BY ordering LIMIT 1 OFFSET your_row;
Related
How can I use offset clause to get all rows except the first row. I am using query like
SELECT * FROM EMPLOYEES ORDER BY SALARY OFFSET 1;
But this is not working in MySQL. what am I doing wrong here?
Sadly in MySQL OFFSET only works together with the LIMIT clause. So you need to use
SELECT * FROM EMPLOYEES ORDER BY SALARY LIMIT 18446744073709551615 OFFSET 1;
or
SELECT * FROM EMPLOYEES ORDER BY SALARY LIMIT 1, 18446744073709551615;
I have chosen that limit number from a different question
MySQL 8.0 can use a window function to do this:
SELECT * FROM (
SELECT *, ROW_NUMBER() OVER (ORDER BY SALARY) AS rownum
FROM EMPLOYEES
) AS t
WHERE rownum > 1
ORDER BY SALARY;
You have to use LIMIT in addition to OFFSET
SELECT * FROM EMPLOYEES ORDER BY SALARY LIMIT 9999 OFFSET 1;
I want to select last 3 rows of my table and no id is defined and table is not sorted either. Put it this way without using order clause or I just want to reverse the records of my non ordered table.
Try this -
SET #rownum:=0;
SELECT *
FROM (SELECT #rownum:=#rownum+1 as rownum,*
FROM yourTable) t1
ORDER BY rownum DESC
LIMIT 3;
I have a scenario where I need to obtain a set of results in ascending order but only given the last ID in the descending sequence.
The order will always be sequentially ascending as the ID is auto generated however, I cannot guarantee that ID sequence is unbroken. i.e. I cannot say 8 - 3 = 5 therefore do a forward lookup from 5.
What is the most efficient was of achieving this. If possibly I want to avoid manipulating the collection in the application logic but can as a last resort.
You could do with an inner select:
SELECT *
FROM (
SELECT *
FROM your_table
WHERE id <= X
ORDER BY id DESC
LIMIT Y
) d
ORDER BY id ASC
You can define your own rownum with query and apply mod to get every 3rd record regardless of it's id, e.g.:
SELECT *
FROM (SELECT id, name, #rownum := #rownum + 1 as `num`
FROM test, (SELECT #rownum := 0) a ) b
WHERE b.num % 3 = 0;
Here's the SQL Fiddle.
I've done research on this problem, but am having trouble finding a solution.
I have the following query that gives me a list of "some_id"s:
SELECT some_id FROM example GROUP BY some_id
And I have the following query that will get a list of the 5 most recent entries for a row that has "some_id" equal to a number.
SELECT * FROM example
WHERE some_id = 1
ORDER BY last_modified DESC
LIMIT 5
How can I get the the top 5 most recent entries from the table "example" for each "some_id", using only one query? If there are less than 5 entries for a "some_id", it is okay to include them, if that makes things less complex.
Many thanks!
Found the answer when looking at the first answer in the following post:
How do I limit the number of rows per field value in SQL?
I've modified it to fit my specific needs:
SELECT * FROM
(
SELECT *, #num := if(#some_id = some_id, #num := #num + 1, 1) as row_num,
#some_id := some_id as some_id
FROM example
ORDER BY last_modified DESC
) as e
WHERE row_num <= 5
Hi Please use Group by and having clause for check limit for every id..
SELECT * FROM `example`
GROUP BY some_id
HAVING count( * ) <= 5
ORDER BY last_modified DESC
It will check for every some_id and check if number of rows for some_id is <=5 then it will be display result.
I have a database table that has two fields , date and name.
I want to have my query pull the first 20 by newest date first, then the rest of the query to pull the other elements by name alphabetically.
So that way the top 20 newest products would show first, then the rest would be ordered by name.
It's a bit ugly, but you can do it in one query:
SELECT name,
`date`
FROM ( SELECT #rank := #rank + 1 AS rank,
name,
`date`
FROM (SELECT #rank := 0) dummy
JOIN products
ORDER BY `date` DESC, name) dateranked
ORDER BY IF(rank <= 20, rank, 21), name;
The innermost query, dummy, initializes our #rank variable. The next derived table, dateranked, ranks all rows by recency (breaking ties by name). The outermost query then simply re-orders the rows by our computed rank, treating ranks greater than 20 as rank #21, and then by name.
UPDATE: This query version is more compact, puts the conditional ranking logic in the outermost ORDER BY, uses IF() rather than CASE/END.
I'm afraid this has to be done by adding a special column to your table or creating a temporary table, TPup. If you let me know whether you are interested in those options, I'll tell you more.
The two queries option like the following might be a possibility, but my version of MySQL tells me LIMIT isn't available in sub-queries.
SELECT `date`, `name` from `table` ORDER BY `date` LIMIT 0, 20;
SELECT `date`, `name` from `table` WHERE `id` NOT IN (SELECT `id` from `table` ORDER BY `date` LIMIT 0, 20) ORDER BY `name`;
Use sql UNION operator to combine result of two SELECT queries.
According to MySQL docs:
use of ORDER BY for individual SELECT statements implies nothing
about the order in which the rows appear in the final result because
UNION by default produces an unordered set of rows.
...
To use an ORDER BY or LIMIT clause to sort or limit the entire UNION
result, parenthesize the individual SELECT statements and place the
ORDER BY or LIMIT after the last one. The following example uses both clauses:
(SELECT a FROM t1 WHERE a=10 AND B=1)
UNION
(SELECT a FROM t2 WHERE a=11 AND B=2)
ORDER BY a LIMIT 10;
Edit:
I missed the part that explain OP needs to sort one set of the result on the date and the other set of the result alphabetically. I think you need to create a temporary field for the sorting purpose. And SQL query would be something similar to this.
(SELECT *, 'firstset' as set_id FROM t1 ORDER BY date LIMIT 0, 20)
UNION
(SELECT *, 'secondset' as set_id FROM t1 ORDER BY date LIMIT 20, 18446744073709551615)
ORDER BY
CASE
WHEN set_id='firstset' THEN date
WHEN set_id='secondset' THEN name
END DESC ;