Select by deleting another Select

Asked

Viewed 48 times

0

well my scenario is this, I have 1 slide where shows the 4 last news

SELECT * FROM noticias WHERE destacar='Destacado' ORDER BY idnoticia DESC LIMIT 4

below the slide I have a list of the other materials where you should present the ones not marked as NOT HIGHLIGHTED and those marked with HIGHLIGHTED from the 4th result, I am using the following query.

SELECT * FROM noticias n WHERE idnoticia NOT IN ( SELECT * FROM (SELECT idnoticia FROM noticias WHERE destacar = 'Destacado' ORDER BY idnoticia ASC LIMIT 10) p) ORDER BY idnoticia DESC LIMIT 9 OFFSET 4

is functioning in parts, this listing from the 4th result but if I include a news in the middle (assuming between the 2nd and 4th HIGHLIGHTED) marked NOT HIGHLIGHTED it does not appear.

exemplo

Some way around this problem?

1 answer

1


Without the bench in hand it becomes difficult to test, but test this way, you don’t need the OFFSET because the first four will be ignored in NOT IN:

SELECT 
  * 
FROM
  noticias 
WHERE idnoticia NOT IN 
  (SELECT 
    idnoticia 
  FROM
    noticias 
  WHERE destacar = 'Destacado' 
  ORDER BY idnoticia DESC 
  LIMIT 4) 
ORDER BY idnoticia DESC 
LIMIT 9 

I hope it helps

Hugs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.