SELECT ID FROM subquery with limit 1000 rows in MySQL - 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.

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"

mysql:how to get max id from duplicated row group by 2 columns?

I have one table name "sections_content" that has the following columns
and I want to get max id for each statecode and policyname
So ,for example,the result of statecode CN / IL /IE should be
How should I write the code for mysql ?
thanks.
I think you can solve this with a JOIN on the MAX for that group:
SELECT * FROM sections_content as t
JOIN
(
SELECT MAX(id) as id
FROM sections_content AS tbl
GROUP BY
tbl.policyName,
tbl.statecode
) AS maxId
ON maxId.id=t.id
I think you can use ORDER BY and LIMIT keywords to get the maximum number.
Exp: Having tables; table1 and table2 as in your database. check below queries.
SELECT table1.id, table2.statecode, table2.policyname, sections_content.student_lga_of_origin
FROM table1
JOIN table2
ON table1.policyname = table2.policyname
ORDER BY table1.id DESC
LIMIT 1
OFFSET 1

Is this an inefficient query?

Assuming table1 and table2 both have a large number of rows (ie several hundred thousand), is the following an inefficient query?
Edit: Order by field added.
SELECT * FROM (
SELECT title, updated FROM table1
UNION
SELECT title, updated FROM table2
) AS query
ORDER BY updated DESC
LIMIT 25
If you absolutely need distinct results, another possibility is to use union all and a group by clause instead:
SELECT title FROM (
SELECT title FROM table1 group by title
UNION ALL
SELECT title FROM table2 group by title
) AS query
group by title
LIMIT 25;
Testing this without the limit clause on an indexed ID column from two tables with ~920K rows each in a test database (at $work) resulted in a bit over a second with the query above and about 17 seconds via a union.
this should be even faster - but then I see no ORDER BY so what 25 records do you actually want?
SELECT * FROM (
SELECT title FROM table1 LIMIT 25
UNION
SELECT title FROM table2 LIMIT 25
) AS query
LIMIT 25
UNION must make an extra pass to fetch the distinct records, so you should use UNION ALL.
Yes, use order by and limits in the inner queries.
SELECT * FROM (
(SELECT title FROM table1 ORDER BY title ASC LIMIT C)
UNION
(SELECT title FROM table2 ORDER BY title ASC LIMIT C)
) AS query
LIMIT 25
This will only go through C rows instead of N (hundreds of thousands). The ORDER BY is necessary and should be on an indexed column.
C is a heuristic constant that should be tuned according to the domain. If you only expect a few duplicates, C=50-100 is probably ok.
You can also find out this for yourself by using EXPLAIN.

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.