我有 3 张桌子:product
,,,order_details
。orders
我想在一个查询中加入他们,显示用户购买的所有产品。
这是我的代码:
SELECT products.id AS id,
products.kode AS kode,
products.sub_kode AS sub_kode,
products.nama AS nama,
products.barcode AS barcode,
products.harga AS harga,
IFNULL(products.keterangan_kemasan, ' ') AS keterangan_kemasan,
IFNULL(products.keterangan_ukuran ,' ') AS keterangan_ukuran,
SUM(IFNULL(order_details.jumlah_pesanan_akhir,0)) AS jumlah,
IFNULL(order_details.jumlah_pesanan_akhir,0) AS jumlah_beli,
orders.idmember AS member
FROM products LEFT JOIN (orders,order_details)
ON (orders.id = order_details.idorder
AND order_details.product_kode = products.kode
AND order_details.product_subkode = products.sub_kode)
WHERE products.nama LIKE '%asdu%'
AND IFNULL(orders.idmember, 21376) = 21376
GROUP BY products.kode, products.sub_kode
ORDER BY IFNULL(order_details.jumlah_pesanan_akhir,0) DESC,
products.nama ASC
LIMIT 30;
发生的情况是,当客户从不购买该商品时,该商品将不会显示。同时,我希望它们显示出来。
如果我从查询中删除它,
AND IFNULL(orders.idmember, 21376) = 21376
一切都会很好地展示出来。但它会显示其他人的订单历史。