Get one before the last created table in MySQL - mysql

Found out this code to give me the lateset created table, but how can I get the one before it?
SELECT TABLE_NAME
FROM information_schema.tables
WHERE table_schema = 'data'
ORDER BY create_time DESC LIMIT 1;

Instead of LIMIT 1, use LIMIT 1,1 this will give your second last created table

Related

Read last record from database without any unique id

I want to read last record from database but i don't have any unique id column. I am using Mysql workbench 6.3c
i tried this query :
SELECT * FROM energymetersdata where VarName='01_lab1_lsi2_kwh'
ORDER BY VarValue desc LIMIT 1;
but i got the first row
Limit ->is used to get the number of record you want to view from table,
'Order by desc'-> orders the column in descending order
first convert your date column date type to str_to_date
SELECT *,str_to_date(timestring,'%d-%m-%Y %T')as date1 FROM energymetersdata where
VarName='01_lab1_lsi2_kwh' ORDER BY date1 desc LIMIT 1;
Try this:
SELECT * FROM energymetersdata where VarName='01_lab1_lsi2_kwh'
ORDER BY TimeString desc LIMIT 1;
Try short query
SELECT * from energymetersdata HAVING MAX(TimeString);
it will return latest record which you want
You appear to have a time column called time_ms. You should use this for getting the most recent value:
SELECT emd.*
FROM energymetersdata emd
WHERE emd.VarName = '01_lab1_lsi2_kwh'
ORDER BY time_ms desc
LIMIT 1;
In particular, this can take advantage of an index on energymetersdata(VarName, time_ms), which should make the query very fast.

How do I select the top 3 starting at row 2?

I never played with the MySQL syntax so I don't know how to do this right now.
I know in SQL it would be
select top 3 cloumn_name
from table_name
MySQL
select column_name
from table_name
limit 3, 2;
can someone explain how limit works?
Use limit and offset:
select t.*
from table_name t
order by ??
limit 3 offset 1;
When using limit you should generally use order by. In addition, for offset purposes, MySQL starts counting at 0 and not 1.

Select rows from database then change a value in a column

I'm using the following query:
select id,link,size from files where status='-1' ORDER BY id DESC LIMIT 10
then I want to change the 'status' column values of the selected rows to -2, is it possible to combine the 'select' and 'update' functions in the same query so that I don't have to query the database again?
You do not need to combine the select with an update:
try:
update files set status='-2' where status='-1' order by id desc limit 10;
The fields names are irrelevant here.

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

Use of Limit in mysql

First query is giving fine results but question is about 2nd
When I have given limit 2,2. How two rows can be selected? this is the weied beahaviour query
SELECT Ordinal_Position, Column_Name FROM information_schema.columns WHERE
table_schema = 'accounts' AND table_name = 'chequeout' LIMIT 2 , 2
What is wrong with 2nd query or its sum bug? What could be the solution?
Expected result of second query is a single row with values 2 and Amount
Limit 2, 2 means start at record 2 (the third 0,1,2) and display 2 rows
What you seem to want to do is :
WHERE Ordinal_Position = 2
You're probably a little confused about how to use LIMIT:
LIMIT <offset>,<limit>
<offset> means starting row index and <limit> tells how much rows.