Retrieve all records with LIMIT in mysql - mysql

I have the following MySQL query;
SELECT records FROM users
LIMIT 1
One record will be retrieved. Suppose I want to retrieve all records but still use the keyword LIMIT. Can I do something like this?
SELECT records FROM users
LIMIT ALL

Just use
SELECT records FROM users
Do not use LIMIT at all
Or if it is required to use LIMIT , use a very high number
SELECT records FROM users LIMIT 999999999999

You have to just make up a ridiculous number and limit it by that.
E.g. SELECT records FROM users LIMIT 18446744073709551615 if you really want to keep the limit clause. This is similar to using offset to infinite amount.

According to mysql doc you are supposed to use a very large number.
SELECT * FROM tbl LIMIT 95,18446744073709551615;
Terrible solution, if you ask me, but its from the mysql documentation
http://dev.mysql.com/doc/refman/5.0/en/select.html#id4651990

If you want to retrieve all data then just use
select * from tbl;

Related

How do I select a record whose timestamp is the most recent

I have a mysql db which one of the columns is of type timestamp. And the records look like this
How do I select the most recent timestamp in a query and display it?
SELECT * FROM tbl ORDER BY created_ts DESC LIMIT 1
PS: you should use hashed passwords rather than plain-text ones
It's simple:
SELECT * FROM TABLE ORDER BY CREATED_TS LIMIT 1
I guess its better to index the sequence ID and get the max from there. Getting max of your timestamp will run into real performance issues when your table grows big.

trimming MySQL table

I would like to trim an existing MySQL ISAM table, 10million records.
What would be a SQL statement to delete the last 5million records? This is just a log table, no damage done by removing records.
delete from table where id >= (select * from (select id from table order by id limit 4999999,1) as t )
You can use ORDER BY and LIMIT with DELETE to affect only a limited number of rows. The way that you would get the 'last' 5 million depends on what you have as far as determining record age, and use that in your ORDER BY.
SELECT *
FROM ??? (youre table name)ORDER BY?????? LIMIT 5000000
Here ??? = youre table name, and ?????? is the way you sort youre table. So if you have some sort of date or number of entry to sort by.
http://www.w3schools.com/sql/
I've used SELECT in my example above so you can try out first if it works correctly! If it does work, change SELECT into DELETE :)
If it doesn't work, than you've to try something else to sort by :)
Does this works
DELETE FROM tableA ORDER BY some_indexed_column DESC LIMIT 1000000

COUNT with LIMIT in mysql query

i need to get total amount of rows when using LIMIT with my query to avoid twice querying.
is it possible?
Use FOUND_ROWS():
For a SELECT with a LIMIT clause, the number of rows that would be returned were there no LIMIT clause
use the statement right after your SELECT query, which needs the CALC_FOUND_ROWS keyword. Example from the manual:
SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name
WHERE id > 100 LIMIT 10;
Note that this puts additional strain on the database, because it has to find out the size of the full result set every time. Use SQL_CALC_FOUND_ROWS only when you need it.

Order of execution of ORDER BY and LIMIT in a MySQL query

I have a query like this where I want to display sorted records in a paginated format.
This is my query.
SELECT * FROM answers
WHERE userid = '10'
ORDER BY votes LIMIT 10, 20
The two arguments to LIMIT will be used for generating page-by-page display of records. Now, my problem is ORDER BY. How does MySQL execute this query?
1st way
Select the records according to filters
Sort them
Limit them
2nd way
Select the records according to filters
Limit them
Sort them
If I think like MySQL engine, I would want to implement 2nd, since I would then have to sort lesser records?
Can somebody throw some light on this?
Regards
A SQL LIMIT will in all databases always work on the result of a query, so it will run the query with the ORDER BY and that result will then be limited. This is a functional requirement, the database not perse needs to execute it like that.
Thus the 1st way.
Mysql follows the 1st way.
If you want your 2nd way, try using a subquery with the limit, then order.
Something like:
SELECT * FROM
(
SELECT * FROM answers
WHERE userid = '10'
LIMIT 10
)
ORDER BY votes
The 1st way as mentioned by the last two answers. From a user perspective, the 2nd way won't be very useful anyways.

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;