I am trying to select the first occurrence from many rows by two keys which are date and shelf.checkID in my query.
SELECT product.productID
, product.Name
, product.date
, product.status
FROM product
INNER JOIN shelf ON product.sheldID=shelf.shelfID
WHERE shelf.checkID = $ID
AND product.date < '$day'
OR (product.date = '$day' AND shelf.expire <= '$time' )
ORDER BY concat(product.date,shelf.expire) DESC
So far i have tried using group by:
(Select *
from (SELECT product.productID
, product.Name
, product.date
, product.status
FROM product
INNER JOIN shelf ON product.sheldID=shelf.shelfID
WHERE shelf.checkID = $ID
AND product.date < '$day'
OR (product.date = '$day' AND shelf.expire <= '$time' )
ORDER BY concat(product.date,shelf.expire) DESC) x
group by attendance.date AND shelf.checkID)
This is the data returned by the first query.
ProductID Date shelfID
1 2017-03-27 2
2 2017-03-27 2
3 2017-03-26 1
4 2017-03-25 3
5 2017-03-25 3
6 2017-03-25 4
7 2017-03-26 1
This is the output am trying to get.
ProductID Date shelfID
1 2017-03-27 2
3 2017-03-26 1
4 2017-03-25 3
6 2017-03-25 4