your query is pretty difficult to read
let me first rewrite it for you --
Code:
SELECT DISTINCT
b.isbn
, b.book_title
, s.price_with_tax
, b.author_name
, b.publisher_company_name
, b.publication_date
, CASE WHEN s.qty>0
OR k.qty>0
THEN 'available'
ELSE 'not available' END AS 'stock'
FROM m_cmn_eng_book b
LEFT OUTER
JOIN m_store_stock s
ON s.isbn = b.isbn
LEFT OUTER
JOIN m_supplier_stock k
ON k.isbn = b.isbn
WHERE b.book_title like '%$mart%'
OR b.author_name like '%$mart%'
OR b.publisher_company_name like '%$mart%'
OR b.isbn like '$mart'
GROUP
BY b.isbn
, b.book_title
, b.author_name
, b.publisher_company_name
, b.publication_date
there is a problem mixing GROUP BY and DISTINCT, they are redundant, so let's just go with GROUP BY
meanwhile, you have s.price_with_tax in the SELECT clause so that might be an issue if different stores have different prices, but we can resolve this using MIN(s.price_with_tax)
also, since you are using GROUP BY, you have the individual values s.qty and k.qty in the SELECT clause, but here there is an easy solution, you just want to know if there are any books in stock, so we can use SUM
Code:
SELECT b.isbn
, b.book_title
, MIN(s.price_with_tax) AS lowest_price
, b.author_name
, b.publisher_company_name
, b.publication_date
, CASE WHEN SUM(s.qty) > 0
OR SUM(k.qty) > 0
THEN 'available'
ELSE 'not available' END AS 'stock'
FROM m_cmn_eng_book b
LEFT OUTER
JOIN m_store_stock s
ON s.isbn = b.isbn
LEFT OUTER
JOIN m_supplier_stock k
ON k.isbn = b.isbn
WHERE b.book_title like '%$mart%'
OR b.author_name like '%$mart%'
OR b.publisher_company_name like '%$mart%'
OR b.isbn like '$mart'
GROUP
BY b.isbn
, b.book_title
, b.author_name
, b.publisher_company_name
, b.publication_date
make sense?
Bookmarks