开发者

How can I write this MYSQL query without using UNION?

开发者 https://www.devze.com 2022-12-18 17:53 出处:网络
I\'d like to do a single query on one table without using UNION Here are the two queries that I need to combine.

I'd like to do a single query on one table without using UNION

Here are the two queries that I need to combine.

SELECT field1, field2, field3 FROM table1 WHERE field4 != 'condition1' AND feild3 >= 'condition2' ORDER BY field3 ASC LIMIT 20;
SELECT field1, field2, field3 FROM table1 WHERE fiel开发者_C百科d4 != 'condition1' AND feild3 < 'condition2' ORDER BY field3 DESC LIMIT 5;

Basically, I'm trying to get 20 records above a certain record (ordered by field3, not the id), and 5 records below it, without grabbing the record itself in the results.

The trick is, I can't use UNION.


You could do something like the following -

select a.*, b.*
from (select field1, field2, field3 
      from table1 
      where field4 != 'condition1' 
        and feild3 >= 'condition2' 
      order by field3 ASC 
      limit 20) a, 
     (select field1, field2, field3 
      from table1 
      where field4 != 'condition1' 
        and feild3 < 'condition2' 
      order my field3 desc
      limit 5) b
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号