Convert SQLITE to MYSQL - 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"

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

mysql duplicate subquery in where clause

I need to do selection from mysql 5.7.22 in one query.
select id from t1 where type_id=(select type_id from t2 where id=1 limit 1) and id not in
(select obj_id from t2
where
type_id = (select type_id from t2 where id=1 limit 1)
and
type2_id = (select type2_id from t2 where id=1 limit
...
)
I have some duplicate subquerys in where clause (it's only part of the query, this subquery duplicates many times)
'(select type_id from t2 where id=1 limit 1)'
Can I some how figure it out in one place, to reduce verbose.
So I want to select once
select type_id, type2_id from t2 where id=1 limit 1
and make type_id, type2_id available in all query context.
I know mysql 8.0 has WITH syntax, but I am using 5.7.22
I want to do this in one query without transactions.
It's hard to give you complete advice without seeing your more of your query. But you have some choices.
You could try creating a view as follows then using it.
CREATE VIEW selector
AS SELECT MAX(type_id) type_id, MAX(obj_id) obj_id
FROM t2
WHERE id = 1
It looks possible that the t2 query returns multiple rows. This view deals with that by using MAX() instead of LIMIT 1. But if t2.id is a primary key, then all you need is
CREATE VIEW selector
AS SELECT type_id, obj_id
FROM t2
WHERE id = 1
Then you can use the view in your query.
For example
SELECT id
FROM t1
WHERE type_id = (SELECT type_id FROM selector)
AND obj_id <> (SELECT obj_id FROM selector)
Or you could figure out how to use join operations rather than subqueries.
SELECT id
FROM t1
JOIN selector ON t1.type_id = selector.type_id AND t1.obj_id <> selector.obj_id
try this
select id from t1 ,(select type_id from t2 where id=1 limit 1) t where type_id=t.type_id and id not in
(select obj_id from t2
where
type_id = t.type_id
and
type2_id = t.type_id
...
)

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.

How do I select the last but 10 rows from a mysql table

I want to run this query
select *
from table
order asc
limit N;
where N is the total number of rows minus 10.
SELECT COUNT(*) FROM (SELECT * FROM table)
returns the total as 189 so, in this case, I would want my limit to be 179
If order is not important, you can use the offset of limit:
Note, there is no actual value for 'Until End Of Table'. The MySQL Documentation suggests to use "some large number" for the second parameter.
SELECT *
FROM table1
order by ID DESC
LIMIT 10, 999999999999999
If you do want in in ascending order you can apply a different ordering afterwards:
SELECT
*
FROM
(SELECT *
FROM table1
ORDER BY ID DESC
LIMIT 10, 999999999999999) x
ORDER BY
ID ASC
Not sure if the most efficient one, but this should work if you only have one field as a primary key.
select *
from T1
where
T1.id not in (
select top(10) id
from T1
order by id desc
)
order by
id;
It will get the last rows by your order and then you can exclude by the key.
Edit:
Better yet, instead of not in, you can use a left outer join.
select
T1.*
from T1
left outer join ( select top(10) id from Enums_Tables order by id desc ) as T2
on
T1.id = T2.id
where
T2.id is null;

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.