Question

I'm using union to combine 2 query. there are some problem when i'm adding ORDER on it.

here's the query as it currently exists

SELECT * 
  FROM (
       SELECT * 
         FROM (
              SELECT nama_barang
                FROM transaksi_jual 

               WHERE kategori_barang.`id_kategori`= "3"  
                 AND id_kampus = "1" 
                 AND (     nama_barang LIKE "%galaxy%" 
                       AND nama_barang LIKE "%young%" )
   ) A ORDER BY date DESC
       UNION 
       SELECT * 
         FROM (
               SELECT A
                 FROM nama_barang 

                WHERE kategori_barang.`id_kategori`= "3"  
                  AND id_kampus = "1" 
                  AND (    nama_barang LIKE "%galaxy%" 
                        OR nama_barang LIKE "%young%" 

                       )
              ) B ORDER BY tanggal DESC
       ) drived LIMIT 0,12

what is the problem with my query? is that no other way to combine 2 sorted query into 1? how to combine 2 sorted query into 1 ?

thanks, i appriciate your help. and sory for my bad grammar.

Was it helpful?

Solution

You ORDER after a UNION, like:

SELECT * FROM table1

UNION

SELECT * FROM table2

ORDER BY id

This ORDER BY orders the whole result, from both table1 and table2

In case you want the records of the first subset first, try:

SELECT *, 1 AS order FROM table1

UNION

SELECT *, 2 AS order FROM table2

ORDER BY order, id

Note: UNION is a lot heavier to execute than UNION ALL, make sure you need to remove double records when you want to use UNION.

OTHER TIPS

I don't think you need a union at all. The two subqueries appear to be the same except for part of the where clause.

          SELECT nama_barang,
                 harga,
                 id_jual,
                 kampus.`nama`,
                 deskripsi,
                 tanggal,
                 baru 
            FROM transaksi_jual 
                 JOIN seller 
                      USING (id_seller)
                 JOIN kategori_barang 
                      USING (id_kategori)
                 JOIN kampus 
                      USING (id_kampus)
                 JOIN subkategori_barang2 
                      ON subkategori_barang2.`id` = transaksi_jual.`id_subkategori2`
                 JOIN subkategori_barang 
                      ON subkategori_barang.`id` = transaksi_jual.`id_subkategori1`
           WHERE kategori_barang.`id_kategori`= "3"  
             AND id_kampus = "1" 
             AND ((     nama_barang LIKE "%%" 
                    AND nama_barang LIKE "%%" 
                    AND nama_barang LIKE "%%" 
                    AND nama_barang LIKE "%%"
                   ) OR
                   (    nama_barang LIKE "%%" 
                     OR nama_barang LIKE "%%" 
                     OR nama_barang LIKE "%%" 
                     OR nama_barang LIKE "%%"
                   )
                  )
           ORDER BY tanngal;

I'm not sure what order you want, but surely you can do it with one query.

EDIT:

I figured that is what you really wanted. To do that, use the following order by on a single query:

order by ((nama_barang LIKE '%%') +
          (nama_barang LIKE '%%') +
          (nama_barang LIKE '%%') +
          (nama_barang LIKE '%%')
         ) desc

This will order by the number of matching keywords. Note that I also changed the string delimited to a single quote. It is a good idea to use single quotes for string and date constants -- and for nothing else.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top