What's wrong with this MYSQL DELETE statement? - mysql

I want to delete records from a table greater than a certain message_id.
MY delete statement doesn't seem to working.
http://sqlfiddle.com/#!2/4f8ee/1
Thanks in advance

DELETE c
FROM `chat_history3` c
inner join
(
select message_id from `chat_history3`
where clan_id=4
ORDER BY message_id DESC
limit 30, 30000
) x
on c.`message_id` < x.`message_id`
SQLFiddle demo

Related

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"

Delete only one record in mysql database

what is the proper way to delete only one record from mysql data base.
this query used for select only one record.
SELECT * FROM Customers WHERE Country='Mexico' ORDER BY Country ASC LIMIT 1;
the above query run correctly .but when replace select to delete that not worked.
DELETE FROM Customers WHERE Country='Mexico' ORDER BY Country ASC LIMIT 1;
how I can fix it?
If you have an id column you can use a subselect. I have removed the order by since this will be the same like order by 'Mexico' asc which is pretty useless.
DELETE FROM Customers
WHERE (CustomerID IN ( SELECT CustomerID
FROM Customers where country = 'Mexico'
ORDER BY CustomerID ASC LIMIT 1 )) ;
I think below query will help you. You will need to have some key ID to differentiate.
DELETE FROM Customers
WHERE SOME_KEY_ID IN
(
SELECT SOME_RANDOM_ID FROM
(
SELECT B.SOME_KEY_ID SOME_RANDOM_ID FROM Customers as B
where Country = 'Mexico'
LIMIT 1
) as c
) ;
Note: The inner select SOME_RANDOM_ID is required, else sqlfiddle throws errors This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery': .
Reference FIDDLE Here

Delete all rows from a MYSQL table except TOP/BOTTOM 50 rows

http://sqlfiddle.com/#!2/d21f3/1
I have a table with some entries here, I want to keep only 50 messages in this table sorted by message_id, and DELETE the rest of entries.
Please help me with the query.
Thanks in advance.
E.g..
DELETE a
FROM chat_history a
LEFT
JOIN
( SELECT x.message_id
FROM chat_history x
JOIN chat_history y
ON y.message_id >= x.message_id
GROUP
BY x.message_id
HAVING COUNT(*) <= 50
) b
ON b.message_id = a.message_id
WHERE b.message_id IS NULL;
http://sqlfiddle.com/#!2/361b4/1
Try this one i have used ORDER BY message_id DESC change it as you want it will delete all except the seleted 50 entries, i have aliased the query because you cannot use same table to select with delete operation
DELETE FROM `chat_history` WHERE id NOT IN ( SELECT t.id FROM
(SELECT id FROM chat_history ORDER BY message_id DESC LIMIT 50 ) t)

mysql delete earlier datas if max(id) - min(id) > 50,000

I have a table in which I need to keep the total number of rows within 50,000. This table includes an Id field (id is auto incremental).
How can I check if max(id) - min(id) > 50,000, then delete the earlier rows?
DELETE FROM news WHERE if (max(id) - min(id) > 50000)
This query will delete all the rows if max(id) - min(id) > 50000, what is the correct way? Ideally I need a one line command, excuse in SSH method. thanks.
DELETE d
FROM news AS d
JOIN
( SELECT MAX(id)-50000 AS lim
FROM news
) AS m
ON d.id < m.lim ;
The above will not leave exactly 50K rows of course, as there may be gaps in the id sequence. But I guess this is expected and not a problem. If you really want to leave exactly 50K rows, any statement will probably be less efficient. You can try this one:
DELETE d
FROM news AS d
JOIN
( SELECT id AS lim
FROM news
ORDER BY id DESC
LIMIT 1 OFFSET 50000
) AS m
ON d.id <= m.lim ;
Delete from news where id < max(id)-50000
DELETE FROM news
WHERE id NOT IN (SELECT id FROM news ORDER BY id DESC LIMIT 50000)

mysql Delete from table after 100

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.