Union - Same table, excluding previous results MySQL

Posted by user82302124 on Stack Overflow See other posts from Stack Overflow or by user82302124
Published on 2012-12-18T23:00:56Z Indexed on 2012/12/18 23:03 UTC
Read the original article Hit count: 154

Filed under:

I'm trying to write a query that will:

  • Run a query, give me (x) number of rows (limit 4)
  • If that query didn't give me the 4 I need, run a second query limit 4-(x) and exclude the ids from the first query
  • A third query that acts like the second

I have this:

(SELECT *, 1 as SORY_QUERY1 FROM xbamZ where state = 'Minnesota' and industry = 'Miscellaneous' and id != '229' limit 4) UNION (SELECT *, 2 FROM xbamZ where state = 'Minnesota' limit 2) UNION (SELECT *, 3 FROM xbamZ where industry = 'Miscellaneous' limit 1)

How (or is?) do I do that? Am I close? This query gives me duplicates

© Stack Overflow or respective owner

Related posts about mysql