I have one table of clockin , in this table i have 600 records. I want to keep the first 250 records and delete the rest of the records.
I want to keep offset value dynamic from mysql query.
DELETE FROM clockin order by id asc limit 250,1000;
DELETE FROM clockin order by id asc limit 263,(SELECT (SELECT COUNT(*) as total FROM clockin)-250);
Does anyone help me.
Related
I'm trying to run a MySQL query as part of a bash script to find out if the last 19 entries all have the same value (and then do something if so). I have an 'id' row that auto increments with each entry and then the row that holds the integer value, 'number'.
My thought was to try the below however this is limiting the number of rows returned to 19 and as the count only returns one row this doesn't work:
mysql --host=localhost --user=uname --database=dbname -s -N -e "select count(*) from table order by id desc limit 19"
I need to limit the actual count itself to only check the last 19 rows. I'm not able to say "where id = x to y" as the table will constantly be growing and so the last 19 id's will always be different.
I would be grateful for any help with this one, thanks!
The limit is not doing anything in your query. The aggregation occurs first, aggregating all the rows to produce one row. Then the limit 19 is applied, which is useless, because there is only one row.
To get the most recent 19 rows, you will want a subquery, so the following will get you a count of at most 19:
select count(*)
from (select t.*
from table t
order by id desc
limit 19
) t;
If you want to get the number of values of some column, you might want count(distinct) instead:
select count(distinct col)
from (select t.*
from table t
order by id desc
limit 19
) t;
Use a subquery.
SELECT COUNT(DISTINCT sq.number)
FROM
(SELECT number FROM TABLE ORDER BY ID DESC LIMIT 19) as sq
As far what I have understood is that you would always be wanting the last 19 records from the table so,
select *
from 'your table name'
order by id desc
limit 19
and if you want distinct values just then type this:
select distinct(column name)
from 'your table name'
order by id desc
limit 19
Hope it helps you.
I want to retrain last 25 entered records and delete remaining records according to id.
DELETE * FROM list
WHERE id NOT IN
(
SELECT *
FROM (
SELECT *
FROM 'list'
ORDER BY id DESC LIMIT 25
) as rows
)
DELETE *
FROM 'list'
WHERE id NOT IN ( SELECT id
FROM 'list'
ORDER BY id DESC
LIMIT 25 )
Deleting while selecting from the same table isn't permitted in MySQL.
You can try something like this:
SELECT #rows_to_delete:=COUNT(*)-25 FROM list;
DELETE FROM list ORDER BY id ASC LIMIT 0, #rows_to_delete;
NB: this is not tested please test before running it on real data.
I think your query is close, but what you need to change is just to filter for id in your subquery, don't select everything, because that doesn't make sense. You want to see if a single item (id) is not in a group of things (all columns).
Try changing your query to this:
DELETE FROM list
WHERE id NOT IN(
SELECT id
FROM list
ORDER BY id DESC
LIMIT 25);
I would test this with some dummy data first, but I believe it will do what you want.
I have a table that has transactions with a datetime column. I'm trying to select the last 'n' records (i.e. 20 rows) but have it sorted oldest to newest.
SELECT *
FROM table
WHERE 1=1
ORDER BY table.datefield DESC
LIMIT 20;
Gives me the 20 most recent, but in the opposite order.
Is this possible in one query, or will I have to do a query to get total rows and then adjust the limit based on that so I can do the table.datefiled ASC and then limit (total rows - n), n
Building a SELECT around your original SELECT and convert this to a derived table should do it
SELECT t.*
FROM (
SELECT *
FROM table
WHERE 1=1
ORDER BY table.datefield DESC
LIMIT 20
) t
ORDER BY t.datefield
I try to program a solution where I have multiple linked tables.
Now I have another problem:
I want to limit count of returned lines to 1000.
But I want to show ID 1-1000, next page 1001-2000.
IDs may be stored in iregular order in database (ID 1 does not have to be the first row)
Not any problem so far:
SELECT * FROM table ORDER BY id ASC LIMIT 1000
BUT
Now I have to sort resulty by another column:
SELECT * FROM table ORDER BY name ASC LIMIT 1000
which might return other IDs than 1-1000
or I do
SELECT * FROM table ORDER BY id ASC, ORDER BY name ASC LIMIT 1000
But this will only sort by ID an then by name. So if I would have any ID as duplicate (which is not possible) I would then have those sorted by name.
How can I achive that I get the first 1000 IDs (some IDs might not exist as they might have been deleted before!) and those thousand rows sorted by name?
Is that even possible?
Try this:
SELECT * FROM table WHERE id IN (
SELECT ID FROM table ORDER BY ID ASC LIMIT 0, 1000
) ORDER BY name ASC
As mentioned in comments that subquery is not supported, 'This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'. Using JOINs is the only alternative left, like this:
SELECT table.* FROM tab JOIN (
SELECT ID FROM table ORDER BY ID LIMIT 1000
) temp
ON table.ID = temp.ID
ORDER BY table.name
I'm trying to get a count of records matching certain criteria within a subset of the total records. I tried (and assumed this would work)
SELECT count(*)
FROM records
WHERE status = 'ADP'
LIMIT 0,10
and I assumed this would tell me how many records of status ADP were in that set of 10 records. It doesn't - it returns, in this case 30, which is the total number of ADP records in the table.
How do I just count up the records matching my criteria including the limit?
SELECT count(*)
FROM ( SELECT records
FROM table
WHERE status = 'ADP'
LIMIT 0,10
)
select count(*) from (select * from records where status='ADP' limit 0,10) as t;