How do I get the max id from the first n id's in a MySQL table? - mysql

I am trying to get the max id of the first n id's in a MySQL database table where the ids are not necessarily sequential. The first n id's are determined by ordering by id ascending. I am using the following query, but this returns the max id in the entire table.
SELECT MAX( id )
FROM files
ORDER BY id ASC
LIMIT 8750000
What am I doing wrong, or ... how do I do this?

SELECT MAX(t.id) FROM
(SELECT id FROM files order by id ASC limit <n>) AS t ;
Of course you will need to replace <n> with an actual value you need.

Related

SELECT SUM(CRC32(column)) LIMIT doesn't work

I need to get the checksum of a specific column and specific number of rows using LIMIT.
SELECT SUM(CRC32(column)) FROM table LIMIT 0, 100;
But, the value returned is the checksum of the entire table. Why does that happen? How can I use LIMIT to get the checksum of only a specific number of rows?
The following works:
SELECT SUM(CRC32(column)) FROM table WHERE id > 0 AND id < 101;
But I don't want to use this method because of potential jumps in the auto_increment value.
Why does that happen?
LIMIT gets applied after aggregate functions.
When used without GROUP BY, SUM aggregates over the whole table, leaving you with only one record, which falls under the LIMIT.
Use this:
SELECT SUM(CRC32(column))
FROM (
SELECT column
FROM mytable
ORDER BY
id
LIMIT 100
) q
You can do
SELECT SUM(CRC32(column)) FROM
(SELECT column FROM table LIMIT 0, 100) AS tmp;

Ordering records by a field only with respect to records with the same ID

I have an EAV table in SQL which as usual has several records for each ID. Each of these records has a numerical value in a column called 'weight' and I'm trying to sort this table so that for each ID the records are ranked in descending order of weight. This is going to be a one off process because I intend to make sure that data is sorted when it goes into the table in future.
Also is the normal protocol for doing something like this to SELECT all of the data, sort it the way you want, and then use the REPLACE command to replace the old data in the table?
I know that I can do this for one id by doing:
SELECT * FROM my_table WHERE id = 'X' ORDER BY weight DESC
but I need to somehow do this for each id in my table. How is this normally done?
you will ALWAYS return the data in the order you wish in this case:
SELECT * from theTable order by id, weight desc
you do not store the data in any particular order. (even if you try this will not matter).
Remove the WHERE clause and add id to ORDER BY. This way, your result set will be ordered first by id, and for each id, it will be ordered by weight.
SELECT * FROM my_table ORDER BY id, weight DESC

Last row in the database

How do you get the last added record/row to the database in MySQL. I have to know this because I only want to update the last added row to the database.
if you have an auto-incrementing id you can do this
select * from your_table order by id desc limit 1
or even simpler
select max(id) from your_table
generally if you have a column in your table that indicates what your last record is do
select max(column_that_indicates_order) from your_table
if you do not have any column indicating what your last record was then you can't find it. Just selecting the first element without any order will not give the lastest entry last.
Edit
To update your last record you can do this:
UPDATE tblPlaces SET lat = '%s', lng = '%s'
order by id desc
limit 1
Assuming you have an ID column within your table you would do this by:
SELECT id FROM table ORDER BY id DESC LIMIT 1;
This is the function for last inserted row when $conn, is the name of your connection. This is only good for last single row.
$thelastid=mysqli_insert_id($conn);
If you want to retrieve the last multiple rows, then you use
select * from your_table order by id desc limit 1

Creating MYSQL rows in ascending order

The following code creates rows in a MYSQL table but returns them in descending order.
How can I make it return values in ascending order?
INSERT INTO
rent
(
id
)
select #s:=#s+1 as seq
FROM (SELECT #s:=399) AS baseview, rent
WHERE #s<1000;
Use an ORDER BY clause using the column that determines the order when you SELECT.
ORDER BY x ASC
Order means nothing when you INSERT. You should not know or care about how it's stored underneath. SQL is declarative - worry about what is done, not how.
If the id field is a Primary Key, they will be returned in ascending order by default. Alternatively, force it with order by id asc.

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;