How to select rows staring from a row whithout limits? - mysql

I want to select rows starting from a row like this:
SELECT * FROM table WHERE Type=1 LIMIT $Start,infinity

If you have a primary key say id, you can use
SELECT * FROM table WHERE Type = 1 AND id >= $Start;

There is nothing to indicate infinity in MySQL. 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:
SELECT * FROM table WHERE Type=1 LIMIT $Start, 99999999999999999999;

Related

Find the last inserted row that matches a query

I want to find the last inserted row that matches a query (ie, find the row that has the largest id among the matching rows.)
for instance, suppose the following query matches 3 rows. with ids, 1,2,and 3. I want to get only the row with id 3.
SELECT * FROM `table` WHERE `mail` = 'someone#example.com'
How do I do this?
You need to write your query like this
SELECT *
FROM table_name
WHERE `mail` = 'someone#example.com'
ORDER BY id DESC
LIMIT 1
Simply use Order By. You sort your result with Id values in decreasing order (that way, you would have maximum ID at the top, in this case 3) and then just limit your result with value 1. That would give you only one row, with max ID. So,
here goes the query:
SELECT * FROM *YourTableName* where mail = '*YourMail*'ORDER BY id DESC LIMIT 1;
Query
SELECT * FROM tbl
WHERE `mail` = 'someone#example.com'
AND id=
(
SELECT MAX(id) FROM tbl
WHERE `mail` = 'someone#example.com'
);
Fiddle demo

select all the rows from my table except the first 20 rows

I want to select all the rows from my table except the first 20 rows. How it possible? The total number of rows are not static.
SELECT statistics_id,title, user_name FROM (
SELECT statistics_id,title, user_name FROM statistics ORDER BY statistics_id DESC
LIMIT(select count(*)from statistics )-20
) sub
ORDER BY access_statistics_id ASC
I know 'LIMIT(select count(*)from statistics )-20' is not a correct method. Please help.
the documentation says (https://dev.mysql.com/doc/refman/5.5/en/select.html) the following:
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;
so you could use something like
LIMIT 20, veryLargeNumber
Try this
DECLARE v_max bigint unsigned default ~0;
SELECT statistics_id,title, user_name
FROM statistics
LIMIT 20, v_max;
After writing the select query u just have to include,
LIMIT 21,100;
21-Offset i.e from which row you want to start selecting and
100- is the Limit[Which you can set according to your need]
You actually need:
SELECT
statistics_id, title, user_name
FROM statistics
ORDER BY
statistics_id ASC
LIMIT 20, 18446744073709551615;
As per MySQL Documentation
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;
If your table grows fast, selecting all rows (with exception of first 20) is not a good idea. In such case, you should batch your query, and handle a subset of entries at a time, some thing like:
SELECT * FROM tbl LIMIT 20,120;
Try using the offset option for the LIMIT syntax. you can read more about the LIMIT syntax at http://dev.mysql.com/doc/refman/5.0/en/select.html.
SELECT `statistics_id`
, `title`
, `user_name`
FROM `statistics`
ORDER BY `statistics_id` ASC
LIMIT 20, 18446744073709551615

Return only one tuple of SQL query result

I want to do a query like
select * from chr2;
but only have MySQL return the first tuple (or an arbitrary) tuple instead of all of them.
How do I do it?
Use the LIMIT clause:
SELECT * FROM chr2 LIMIT 1;
If you want an arbitrary row returned, you have to sort your rows by an random col like this (MySQL docu):
SELECT * FROM chr2
ORDER BY RAND()
LIMIT 1;
On large tables, however, you might run into performance problems with this, as there a random value has to be created for each row and the table has to be sorted according to this column.
Try this ::
select * from chr2 limit 1

how to specify only start index in LIMIT in MySQL select query?

I want to select the records from the table starting from 3rd row. How this can achieved with LIMIT?
From the MySQL Documentation:
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;
Like this:
SELECT * FROM MyTable LIMIT 2, 18446744073709551615;
(The offset is zero-based)
i am giving syntax
SELECT field1, field2,...fieldN table_name1, table_name2...
[WHERE Clause]
[OFFSET M ][LIMIT N]

Omit the first 5 rows?

I want to SELECT all rows except for the first 5 rows in a table.
How do I do that?
Why can't I just type
$query = "SELECT *
FROM ages
OFFSET 5
ORDER BY id ASC";
SELECT * FROM tbl LIMIT 5,18446744073709551615;
from http://dev.mysql.com/doc/refman/5.0/en/select.html
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;
In Oracle:
select name, price
from items
where rownum > 5
Here's a solution using variables - just add your order by clause and you should be set.
set #n=-1
select * from TABLE where (#n:=#n+1) >= 5;
I just typed:
$query = "SELECT *
FROM ages
LIMIT 100
OFFSET 10";
Why couldn't anybody give me such an easy answer? :)