I have 2 tables, Resource
and views
and I'm trying to get the list of all the items from Resource
table whose ran_resId
don't match with the ran_resId
of last 2 records of views
table. I'm trying to use LIMIT
in my sub-query which is giving this error:
This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'`
Here's my query –
select r.res_id, r.res_name
from Resource AS r
where r.ran_resId NOT IN
( select v.ran_resId
from views AS v
order by v.viewsid
limit 2
)
group by r.ran_resId
I've google it and found out that LIMIT can't be use in SubQuery in MySql, And as an alternate I could use JOIN however I'm not able to create the query to get the desired results.
A help is highly appreciated.
PS – I'm using MySql 5.6
Best Answer
If
2
is a fixed value and you'll never need to compare against 3, 4 or more items in the future, then you could convert the subquery to a derived table and find the 2 values with aggregation:A more general solution (where you can use any
LIMIT
and not just 2), is to write the antijoin with theLEFT JOIN
/IS NULL
method:An
NOT EXISTS
version should work as well but I'm not sure that it would be very efficient, due to the double nesting:Also note that I removed the last
group by r.ran_resId
from your query.It makes no sense to
group by a
andselect b, c
in a query. And while MySQL allows it, it is a guaranteed way to get garbage results.