How to select rows above # - mysql

I'm currently trying to setup a query that only selects from a row number and upwards from it. I've tried to use the LIMIT method in this way:
SELECT name FROM names LIMIT 1500, *
Which I expected to select from row number 1500 till the table's rows ended, but got a MySQL error instead.
I then tried to use a conditional for the ID of the rows like so:
SELECT name FROM names WHERE id >= 1500
Which gave unpredictable behavior since there are rows that get deleted, so it's not taking the real row numbers.

I think offset will do what you want. Unfortunately, MySQL requires a LIMIT value, but you can just put in a ridiculous number:
SELECT name
FROM names
OFFSET 1499
LIMIT 999999999;

you could use a subquery
select name
from names
where id > (
select max(id)
from names
order by names
limit 1500
)

Related

Retrieve a single distinct value each time the query is ran

i have a column full of resort id's say 44 rows, i am using the following query
Query-> SELECT DISTINCT RESORT ID FROM Schema.table Name WHERE Condition='Value' AND ROW NUMBER= 1
the above query returns one value say='15'
when i run it multiple it is returning the same value ='15'!!!
i require a different value each time the query is ran
could any one please help me out.
Thanks,
You will get Random Resort ID by using this
SELECT TOP 1 ResortID FROM TableName
-- WHERE <Condition>
ORDER BY NEWID() ASC

How can I select the second last and last row from the database?

When we are retrieving data from database, we use something like this
SELECT * FROM Names
However, we will get all the data inside the specific table. Since I am going to update some data to the database and want to make a comparison bewteen the last row of data in the db and the most updated data, how can I select and retrieve the last two row of the database only?
If you were using SQL Server, you would do something like this:
SELECT TOP 2 * FROM Names ORDER BY Name DESC
SQL Server syntax:
SELECT TOP 2 column_name FROM table_name ORDER BY column_name DESC;
Example:
If you want to retrieve the last value of the customer_name column from the customers table, you need to write the following query:
SELECT LAST (CUSTOMER_NAME) AS LAST_CUSTOMER FROM CUSTOMERS;
You should include a column in the Names table to keep track of when a name was added to the table since you cannot guarantee that the rows are in the order that they were inserted. With that column you can use the order by clause..
In MySQL Syntax:
SELECT *
FROM Names
ORDER BY order_column DESC
LIMIT 2;
If you want to get the last rows as they are, you cannot order the table by the inserted names because that is just getting the 2 names that come last in an alphabetically sorted list of names. You could try something like this where you include an offset in the limit clause if you get the number of rows in the table:
SELECT *
FROM Names
LIMIT *num_rows*-2, 2;
You would have to know the number of rows to use this query or use an additional query to implement a row count that works with limit. This, however, still may not be accurate since the system may not order the rows as they were inserted. I still recommend the first option where you keep track of order/time a name was inserted.

Grab a certain amount of database entries from a table

Is there a way to grab an exact amount of entries from a database example. For example say you had a table that just had an id and total visits for the columns. Say you wanted to grab exactly 20 entries and sort them by total visits. How would you go about this? I know how to sort the whole table, but would like to be able to grab the top twenty total visits and then sort them. Thanks
O and right now I am using sqlite, but I know in the future I will be using mysql also. Thanks
Try with:
SELECT * FROM TableName ORDER BY TotalVisits LIMIT 20
using limit to get the top 20,
and if you want to add another sort, add it after visit column
like :
SELECT * FROM mytable ORDER BY visits DESC
/*here put another order by field like date */
, date
LIMIT 20
Use ORDER - LIMIT clause
SELECT * FROM table ORDER BY field [ASC|DESC] LIMIT 20 OFFSET [offset value]
You need to use LIMIT, but you will need to put the whole thing in a subquery if you intend to re-sort the top 20 based on separate criteria. So
SELECT * from <table> order by <total visits column> LIMIT 20
will get you the top 20, but then to sort within that result you would do something like
SELECT * from
(SELECT * from <table> ORDER BY <total visits column> LIMIT 20)
ORDER BY <other criteria>
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, with these exceptions:
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
In other words, LIMIT row_count is equivalent to LIMIT 0, row_count.
All on: http://dev.mysql.com/doc/refman/5.5/en/select.html better explnation for mysql, however sqlite works same way: http://www.sqlite.org/lang_select.html

MySQL skip first 10 results

Is there a way in MySQL to have the first 10 result from a SELECT query skipped?
I'd like it to work something like LIMIT.
Use LIMIT with two parameters. For example, to return results 11-60 (where result 1 is the first row), use:
SELECT * FROM foo LIMIT 10, 50
For a solution to return all results, see Thomas' answer.
There is an OFFSET as well that should do the trick:
SELECT column FROM table
LIMIT 10 OFFSET 10
OFFSET is what you are looking for.
SELECT * FROM table LIMIT 10 OFFSET 10
From the manual:
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;
Obviously, you should replace 95 by 10. The large number they use is 2^64 - 1, by the way.
LIMIT allow you to skip any number of rows. It has two parameters, and first of them - how many rows to skip
To skip first 10 rows use OFFSET 10, but LIMIT is also required,
If you want to get all the rows and only skip first 10 rows try using a big number like 999999999 or as per your choice
SELECT * FROM table LIMIT 999999 OFFSET 10
select * from table where id not in (select id from table limit 10)
where id be the key in your table.
If your table has ordering by id, you could easily done by:
select * from table where id > 10

What's the most efficient way to select the last n rows in a table without changing the table's structure?

What's the most efficient way to select the last n number of rows in a table using mySQL? The table contains millions of rows, and at any given time I don't know how large the table is (it is constantly growing). The table does have a column that is automatically incremented and used as a unique identifier for each row.
SELECT * FROM table_name ORDER BY auto_incremented_id DESC LIMIT n
Actually the right way to get last n rows in order is to use a subquery:
(SELECT id, title, description FROM my_table ORDER BY id DESC LIMIT 5)
ORDER BY tbl.id ASC
As this way is the only I know that will return them in right order. The accepted answer is actually a solution for "Select first 5 rows from a set ordered by descending ID", but that is most probably what you need.
(Similar to "marco"s answer,)
my fav is the max()-function of MySQL too, in a simple one-liner, but there are other ways of sure:
SELECT whatever FROM mytable WHERE id > (SELECT max(id)-10 FROM mytable);
... and you get "last id minus 10", normally the last 10 entries of that table.
It's a short way, to avoid the a error 1111 ("Invalid use of group function") not only if there is a auto_increment-row (here id).
The max()-function can be used many ways.
Maybe order it by the unique id descending:
SELECT * FROM table ORDER BY id DESC LIMIT n
The only problem with this is that you might want to select in a different order, and this problem has made me have to select the last rows by counting the number of rows and then selecting them using LIMIT, but obviously that's probably not a good solution in your case.
Use ORDER BY to sort by the identifier column in DESC order, and use LIMIT to specify how many results you want.
You would probably also want to add a descending index (or whatever they're called in mysql) as well to make the select fast if it's something you're going to do often.
This is a lot faster when you have big tables because you don't have to order an entire table.
You just use id as a unique row identifier.
This is also more eficient when you have big amounts of data in some colum(s) as images for example (blobs). The order by in this case can be very time and data consuming.
select *
from TableName
where id > ((select max(id) from TableName)-(NumberOfRowsYouWant+1))
order by id desc|asc
The only problem is if you delete rows in the interval you want. In this case you would't get the real "NumberOfRowsYouWant".
You can also easily use this to select n rows for each page just by multiplying (NumberOfRowsYouWant+1) by page number when you need to show the table backwards in multiple web pages.
Here you can change table name and column name according your requirement . if you want to show last 10 row then put n=10,or n=20 ,or n=30 ...etc according your requirement.
select * from
(select * from employee
Order by emp_id desc limit n)
a Order by emp_id asc;