mysql Delete from table after 100 - mysql

Now I am trying to keep 100 messages of one user in the database and I am trying to delete the another messages out of 100
my database is mysql. my sql is
DELETE FROM userMessage WHERE id = ? limit 100, 9999;
but the version of my database don`t support this sql.
thanks:)

Maybe something like this?
DELETE FROM userMessage WHERE id not in
(SELECT id FROM userMessage uM where user_id = ? ORDER BY id DESC LIMIT 100)

Why not select 100 records into a temp table. Then delete the old table and rename the temp table name afterwards.

Having unique id you can use this query -
DELETE t1
FROM
table1 t1
LEFT JOIN (SELECT * FROM table1 ORDER BY id LIMIT 100) t2 -- Specify your LIMIT values here
ON t1.id = t2.id
WHERE
t2.id IS NULL
This query will delete all records after 100.

Related

Delete records from table, if it exceeds 100k records

I have a condition, where in Audit logs, if the records exceeds 100k, then delete the previous old records, I dont want to delete all the 100k records, but want to delete only old records, I want to maintain the latest 100k records.
Below is a query i have tried, please anyone help me, how to prepare the query.
DELETE FROM audit_logs where
id not in (SELECT id from audit_logs order by ID DESC LIMIT 100000);
You could wrap into another select the subquery;
DELETE FROM audit_logs
WHERE id not in (SELECT t1.id
FROM ( SELECT id
FROM audit_logs
ORDER BY ID DESC
LIMIT 100000
) as t1
);
Or use NOT EXISTS :
DELETE FROM audit_logs a1
WHERE NOT EXISTS ( SELECT *
FROM ( SELECT id
FROM audit_logs a2
ORDER BY ID DESC
LIMIT 100000
) as t1
);
Read more on : https://dev.mysql.com/doc/refman/8.0/en/update.html

if I want to delete some data in mysql, can I use delete from table where xxx='xxx' and indexID not in (select * table a where xxx='xxx limit 3)?

if I want to delete some data in mysql, can I use
delete from table where xxx='xxx' and indexID not in (select * table where xxx='xxx limit 3)??
because when I used this code, I got the error.
I know how to working in mssql,
DELETE FROM table WHERE xxx = 'xxx' AND indexID NOT IN (SELECT TOP(3) indexID FROM table WHERE xxx = 'xxx' ORDER BY check_date DESC

Convert SQLITE to MYSQL

Can someone help me convert this into MYSQL since IN is not supported in MYSQL . Should i use INNER JOIN? but how?
DELETE from SSLDOMAINS_logstable where id IN (SELECT id from SSLDOMAINS_logstable order by id asc limit 50)
You only need the ORDER BY clause and LIMIT in MySql:
DELETE FROM SSLDOMAINS_logstable
ORDER BY id
LIMIT 50
This query will delete the first 50 rows of the table ordered by id ascending.
DELETE t1.*
FROM SSLDOMAINS_logstable t1
JOIN ( SELECT t2.id
FROM SSLDOMAINS_logstable t2
ORDER BY id ASC
LIMIT 50 ) t3 ON t1.id = t3.id
If id is unique (including primary), see forpas's solution. But if not... subquery selects least 50 id values, query deletes records with these id values - i.e. LIMIT in this case works like "LIMIT WITH TIES"

SELECT ID FROM subquery with limit 1000 rows in MySQL

I want to select 1000 rows from another table using the last 1000 ID from another table. This is the query but it returned an error message. What did I do wrong with this query?
SELECT * FROM table1
WHERE id IN
(
SELECT id FROM table2
LIMIT 50
)
Error message received.
Error Code : 1235
This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
SELECT *
FROM table1 x
JOIN
( SELECT id FROM table2 ORDER BY id LIMIT 50 ) y
ON y.id = x.id;
You should join both tables and sort in descending order and then apply the limit.
SELECT table1.* FROM table1
JOIN table2 USING (id)
ORDER BY id DESC
LIMIT 1000;
This will give you 1000 entries with highest idin descending order if, and only if, they exist in both tables.
It is easier to do it like this:
SELECT * FROM TBL1, TBL2
WHERE TBL2.FK_ID = TBL1.ID
ORDER BY TBL2.ID
ASC LIMIT 0,1000
you can only do it if the second table has table1's id as a foreign key, but still has its own id identifying the order of when they are created.
Not sure about how to use the limit though.
I hope it helps.

Best way of deleting SQL rows how i want

How can i construct a SQL query to delete how i want.
I have two tables.
Table 1.
ID: Some Random Not Significant To This Question Columns : DateTime : UserID
Table 2.
ID: Some Random Not Significant To This Question Columns : DateTime : UserID
The two tables are related by DateTime and UserID
Is there anyway i can create a query so that it deletes from table 2 if no rows in table1 have a matching DateTime & UserID.
Thanks
You can use LEFT JOIN :
DELETE table2
FROM table2 t2 LEFT JOIN table1 t1 ON t1.`DateTime` = t2.`DateTime`
AND t1.`UserID` = t2.`UserID`
WHERE t1.`UserID` IS NULL
DELETE
FROM table2 t2
WHERE NOT EXISTS
(
SELECT NULL
FROM table1 t1
WHERE (t1.userId, t1.dateTime) = (t2.userId, t2.dateTime)
)
First of all: create a backup before you delete lots of records :)
The idea:
DELETE FROM
table1
WHERE
NOT EXISTS (SELECT 1 FROM table2 WHERE table1.referenceColumn = table2.referenceColumn)
You can check which records will be deleted by replacing the DELETE with SELECT *
And now the solution
DELETE FROM
table2
WHERE
NOT EXISTS (
SELECT 1 FROM
table1
WHERE
table2.UserID = table1.UserID
AND table2.DateTime = table1.DateTime
)