简体   繁体   中英

how to display the smallest data in phpmyadmin

I need help to get the solution for this condition. This is my query :

   SELECT a.idbarang, a.nama,b.stokbagus, a.hargaJual , b.exp
   FROM barang a 
   LEFT JOIN detail_barang b on a.idbarang = b.idbarang 
   WHERE a.status = '1'

the result :

  idbarang   nama              stokbagus   hargaJual  exp
  --------   ----------------  ---------   ---------  ----------
  33001      Pepsi Can 330 Ml     30         900      2015-06-17
  21001      Cheetos 10gr         30         900      2015-12-19
  21001      Cheetos 10gr         25         900      2014-12-07

how to display one cheetos that have the smallest 'exp'. I want to display

  idbarang   nama              stokbagus   hargaJual  exp
  --------   ----------------  ---------   ---------  ----------
  33001      Pepsi Can 330 Ml     30         900      2015-06-17
  21001      Cheetos 10gr         25         900      2014-12-07

You can order by exp and set the limit to 1, that way only the lowest exp will be shown. It would look like this:

SELECT a.idbarang, a.nama,b.stokbagus, a.hargaJual , b.exp FROM barang a LEFT JOIN detail_barang b on a.idbarang = b.idbarang
WHERE a.status = '1' ORDER BY b.exp LIMIT 1;

If you want to display the smallest exp for each nama , you need to use GROUP BY . Try this:

SELECT a.idbarang, a.nama,b .stokbagus, a.hargaJual, MIN(b.exp)
FROM barang a
LEFT JOIN detail_barang b ON a.idbarang = b.idbarang
WHERE a.status = '1'
GROUP BY a.idbarang

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM