我试图通过我的查询中的date和shelf.checkID两个键从多行中选择第一个匹配项。
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
到目前为止,我尝试过使用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)
这是第一个查询返回的数据。
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
这是我想要获得的输出。
ProductID Date shelfID
1 2017-03-27 2
3 2017-03-26 1
4 2017-03-25 3
6 2017-03-25 4