Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 0.96 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Using limit clause along with union all or selecting top 2 and last 3 results
  2. select *
  3.     from tbl_product asc
  4. union
  5. select *
  6.     from tbl_product desc
  7.     order by pd_price limit 2
  8.        
  9. SELECT * FROM (
  10.     SELECT *
  11.     FROM tbl_product
  12.     ORDER BY pd_price ASC
  13.     LIMIT 2
  14. ) tbl1
  15.  
  16. UNION ALL
  17.  
  18. SELECT * FROM (
  19.     SELECT * FROM (
  20.         SELECT *
  21.         FROM tbl_product
  22.         ORDER BY pd_price DESC
  23.         LIMIT 3
  24.     ) tbl_a
  25.     ORDER BY pd_price ASC
  26. ) tbl2
  27.        
  28. select *
  29.         from (
  30.             SELECT *
  31.                 FROM `tbl_product`
  32.                 order by pd_price desc limit 2
  33.         ) as t1
  34.  
  35. union all
  36.  
  37. select *
  38.     from (
  39.         SELECT *
  40.             FROM `tbl_product`
  41.             order by pd_price asc limit 3
  42.     )  as t2
  43.        
  44. CREATE TEMPORARY TABLE IF NOT EXISTS `tmp_table` ENGINE=MEMORY SELECT `field` FROM `tbl` ORDER BY `id` ASC LIMIT 2;
  45. INSERT INTO tmp_table SELECT `field` FROM `tbl` ORDER BY `id` DESC LIMIT 3;
  46. SELECT field FROM tmp_table;