All rows after the nth row? - mysql

If I specify a number, say 5, what query will give me all the rows after the 5th row? Like,
SELECT * FROM table WHERE 1=1;
only I want it to exclude the top 5. Thanks.

Use the limit with a very high number as the second argument.
select * from myTable limit 5,18446744073709551615;
From MySQL Docs:
To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:
SELECT * FROM tbl LIMIT 95,18446744073709551615;
BTW: You don't need WHERE 1=1. It doesn't add any value to the query, just clutter.

SELECT * FROM table ORDER BY somecolumn LIMIT 5,1000
http://dev.mysql.com/doc/refman/5.1/en/select.html
[LIMIT {[offset,] row_count | row_count OFFSET offset}]

Are you looking for LIMIT?
SELECT * FROM table LIMIT 5,9999999
The second parameter to limit is just a large number to get all rows. Adjust accordingly.
See: http://dev.mysql.com/doc/refman/5.5/en/select.html

If you have a column whose values can be ordered (and are unique), say ID1, You can do it in pure SQL (e.g. not using the MySQL specific LIMIT) as follows (Syntax is Sybas-ey, may need to tweak table alias and joins to work on mySQL):
SELECT * FROM table WHERE ID1 not in
-- SELECT FIRST #N ROWS IN ACCENDNING ORDER
(SELECT t1.ID1 FROM table 't1', table 't2'
WHERE t1.ID1 < t2.ID2
GROUP BY t1.ID1
HAVING count(*) <= #N)

Related

How to get the first item and the last item in a single sql statement

I was wondering how to grab the first item and the last item in a MySQL database. I know these two lines will do the work, but I am trying to limit it down to one line.
SELECT * FROM `test_table` order by ID asc limit 0,1
SELECT * FROM `test_table` order by ID desc limit 0,1
For example there is a table called test_table and it has 5 rows. I would like to get row number 1 and row number 5 in the same statement. This will save me from using many PHP if statements and will shorten up my code. Any help would be appreciated.
The following SQL does not get the first item:
SELECT * FROM `test_table` limit 0,1
It returns an arbitrary row. SQL tables and result sets are unordered, so limit without order by returns an indeterminate row.
The syntax to do what you want is:
(SELECT t.* FROM `test_table` t ORDER BY id ASC LIMIT 1)
UNION ALL
(SELECT t.* FROM `test_table` t ORDER BY id DESC LIMIT 1);
Note: If the table has only one row, this returns two rows, because the same row is the "first" and "last". If you want only one in this situation, you could use UNION rather than UNION ALL.
use union it will combine both sql queries and you the expected result
SELECT * FROM `test_table` limit 0,1
union
SELECT * FROM `test_table` order by ID desc limit 0,1
You can use the min and max ID if it's monotonically increasing -
SELECT * FROM `test_table`
WHERE ID IN ((SELECT MIN(ID) FROM `test_table`), (SELECT MAX(ID) FROM `test_table`));
I'd suggest running the explain for the query to see if it performs as per your expectation.

Mysql SELECT function inside mysql LIMIT function

SELECT COUNT('2018-02-12')
as ids FROM tg_partner_data
WHERE assign_to = '2'
AND
followup_date = '2018-02-12' AND active='Y'
LIMIT (SELECT count FROM tg_master_count WHERE count_for = 'followup')
I tried to get count as output but this query doesnot works . I am using like this because the limit setting is dynamic.
An immediate problem with your use of LIMIT is that you have no ORDER BY clause. Which first records do you expect from the result set? This is not clear. I will give a generic pattern which you can use to have a subquery limit the number of records.
Consider a simple table with just one column:
col
1
2
3
4
5
Now let's say that you want to limit the number of records in the result set, based on an ordering of col, using some subquery. We can write the following:
SET #rn=0;
SELECT col
FROM
(
SELECT
col,
#rn:=#rn+1 rn
FROM yourTable
ORDER BY
col
) t
WHERE t.rn <= (SELECT 3 FROM dual); -- replace with your own subquery
Demo
The basic idea here is that we simulate row number in a subquery, that is, we assign a row number to each record, ordered according the the col column. Then we subquery that table, and retain only records matching the subquery you want to use to limit.

Select the ROW having the Maximum Value of a certain Column

For example, if my query returns two rows or more, I would like to select the row having the most recent date.
I'm doing something like this:
SELECT * FROM Table1 WHERE Name=Mark AND MAX(TIMESTAMP(date(str_to_date(DATE_REGISTERED,'%d/%m/%Y'))))
The error returned is:
#1111 - Invalid use of group function
To limit your results to only the most recent (based on a field named DATE_REGISTERED as per your example) you would do
SELECT * FROM Table1 WHERE Name='Mark' ORDER BY STR_TO_DATE(DATE_REGISTERED,'%d/%m/%Y') DESC LIMIT 1
you should use the order by statement:
SELECT * FROM Table1 WHERE Name=Mark
order by str_to_date(DATE_REGISTERED,'%d/%m/%Y') DESC LIMIT 1

Select the first row from two tables

I know MySQL doesn't have a TOP 1 function for selecting the the first row. So I use limit 1 to do it. But how can I select the first row from two tables (tableX, tableY) at the same time.
like this?
SELECT * FROM Database.tableX
limit 1, Database.tableY limit 1
Probably, if the table definitions match:
SELECT * FROM Database.tableX LIMIT 1
UNION ALL
(
SELECT * FROM Database.tableY LIMIT 1
)
Note the inner bracket is necessary. Else the optimizer will limit the result set to 1 seeing the last LIMIT 1 clause
This is not possible unless there is some kind of relation between the two rows.
The other answers here try to join two rows, but they give no guarantee that it is the first row in each table. They also assume there is a field that can be matched from the two tables.
My best suggestion is to do two queries:
SELECT * FROM Database.tableX limit 1;
SELECT * FROM Database.tableY limit 1;
EDIT: you should also add a ORDER BY to tell MySQL how to sort the rows.
Just this:
SELECT *
FROM Database.TableX, Database.TableY
LIMIT 1
or maybe this?
SELECT *
FROM
(SELECT * FROM Database.TableX LIMIT 1) t1,
(SELECT * FROM Database.TableY LIMIT 1) t2

How to select last N records from a table in mysql

This code can be used to select the first ten records from a table in mysql. How can I do the same to display last ten records from a table which has 1000 records. I want to display the name in asc order dont want to change that.
SELECT name, cost FROM test orderby name asc LIMIT 10 ;
SELECT q.name, q.cost
FROM (SELECT name, cost
FROM test
ORDER BY name DESC LIMIT 10) q
ORDER BY q.name ASC;
The LIMIT clause can take two parameters, which will provide an offset:
The LIMIT clause can be used to constrain the number of rows returned
by the SELECT statement. LIMIT takes one or two numeric arguments,
which must both be nonnegative integer constants (except when using
prepared statements).
With two arguments, the first argument specifies the offset of the
first row to return, and the second specifies the maximum number of
rows to return. The offset of the initial row is 0 (not 1):
SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15 To retrieve all
rows from a certain offset up to the end of the result set, you can
use some large number for the second parameter. This statement
retrieves all rows from the 96th row to the last:
SELECT * FROM tbl LIMIT 95,18446744073709551615; With one argument,
the value specifies the number of rows to return from the beginning of
the result set:
SELECT * FROM tbl LIMIT 5; # Retrieve first 5 rows
http://dev.mysql.com/doc/refman/5.0/en/select.html
So with this:
SELECT name, cost FROM test orderby name asc LIMIT 990, 10;
SELECT name, cost
FROM (SELECT name, cost FROM test orderby name desc LIMIT 10) as test
ORDER BY name asc;
You can use ROW_NUMBER() clause for this, this will be helpful for you
select top "N" * from (select ROW_NUMBER() OVER(Order by Col_Name desc) as RowNo,* from Table_Name) Table_Name