SELECT SQL_CALC_FOUND_ROWS
a.ProductID,a.ProductName,a.ExpiryDate,a.ExpiryUnit,b.ProductionDate,b.WareHouseID,b.BinID,b.ShelfID
FROM product a
LEFT JOIN warehouseinventory b ON a.ProductID=b.ProductID
WHERE DATE_ADD(b.ProductionDate,INTERVAL a.ExpiryDate DAY)<=NOW() and a.CompanyID=@CompanyID
LIMIT 0,10;
SELECT FOUND_ROWS() AS TotalRecords;
SELECT CASE ExpiryUnit
WHEN 1 then
(SELECT a.ProductID
FROM product a
LEFT JOIN warehouseinventory b ON a.ProductID=b.ProductID
WHERE DATE_ADD(b.ProductionDate,INTERVAL a.ExpiryDate DAY)<=NOW() and a.CompanyID=@CompanyID
)
ELSE '' END
AS t
FROM product
实在是太菜了,求助大神!!
5 个解决方案
#1
case when 可以多次判断case when ....then.... when.... then....else ...end
SELECT SQL_CALC_FOUND_ROWS
a.ProductID,a.ProductName,a.ExpiryDate,a.ExpiryUnit,b.ProductionDate,b.WareHouseID,b.BinID,b.ShelfID
FROM product a
LEFT JOIN warehouseinventory b ON a.ProductID=b.ProductID
WHERE ( a.ExpiryUnit=1 AND DATE_ADD(b.ProductionDate,INTERVAL a.ExpiryDate DAY)<=NOW() )
OR ( a.ExpiryUnit=2 AND DATE_ADD(b.ProductionDate,INTERVAL a.ExpiryDate MONTH)<=NOW())
OR ( a.ExpiryUnit=3 AND DATE_ADD(b.ProductionDate,INTERVAL a.ExpiryDate YEAR)<=NOW() )
AND a.CompanyID=@CompanyID
LIMIT 0,10;
SELECT FOUND_ROWS() AS TotalRecords;
SELECT SQL_CALC_FOUND_ROWS
a.ProductID,a.ProductName,a.ExpiryDate,a.ExpiryUnit,b.ProductionDate,b.WareHouseID,b.BinID,b.ShelfID
FROM product a
LEFT JOIN warehouseinventory b ON a.ProductID=b.ProductID
WHERE ( a.ExpiryUnit=1 AND DATE_ADD(b.ProductionDate,INTERVAL a.ExpiryDate DAY)<=NOW() )
OR ( a.ExpiryUnit=2 AND DATE_ADD(b.ProductionDate,INTERVAL a.ExpiryDate MONTH)<=NOW())
OR ( a.ExpiryUnit=3 AND DATE_ADD(b.ProductionDate,INTERVAL a.ExpiryDate YEAR)<=NOW() )
AND a.CompanyID=@CompanyID
LIMIT 0,10;
SELECT FOUND_ROWS() AS TotalRecords;
SELECT SQL_CALC_FOUND_ROWS
a.ProductID,a.ProductName,a.ExpiryDate,a.ExpiryUnit,b.ProductionDate,b.WareHouseID,b.BinID,b.ShelfID
FROM product a
LEFT JOIN warehouseinventory b ON a.ProductID=b.ProductID
WHERE ( a.ExpiryUnit=1 AND DATE_ADD(b.ProductionDate,INTERVAL a.ExpiryDate DAY)<=NOW() )
OR ( a.ExpiryUnit=2 AND DATE_ADD(b.ProductionDate,INTERVAL a.ExpiryDate MONTH)<=NOW())
OR ( a.ExpiryUnit=3 AND DATE_ADD(b.ProductionDate,INTERVAL a.ExpiryDate YEAR)<=NOW() )
AND a.CompanyID=@CompanyID
LIMIT 0,10;
SELECT FOUND_ROWS() AS TotalRecords;
SELECT SQL_CALC_FOUND_ROWS
a.ProductID,a.ProductName,a.ExpiryDate,a.ExpiryUnit,b.ProductionDate,b.WareHouseID,b.BinID,b.ShelfID
FROM product a
LEFT JOIN warehouseinventory b ON a.ProductID=b.ProductID
WHERE ( a.ExpiryUnit=1 AND DATE_ADD(b.ProductionDate,INTERVAL a.ExpiryDate DAY)<=NOW() )
OR ( a.ExpiryUnit=2 AND DATE_ADD(b.ProductionDate,INTERVAL a.ExpiryDate MONTH)<=NOW())
OR ( a.ExpiryUnit=3 AND DATE_ADD(b.ProductionDate,INTERVAL a.ExpiryDate YEAR)<=NOW() )
AND a.CompanyID=@CompanyID
LIMIT 0,10;
SELECT FOUND_ROWS() AS TotalRecords;
#3
case when 可以多次判断case when ....then.... when.... then....else ...end
SELECT SQL_CALC_FOUND_ROWS
a.ProductID,a.ProductName,a.ExpiryDate,a.ExpiryUnit,b.ProductionDate,b.WareHouseID,b.BinID,b.ShelfID
FROM product a
LEFT JOIN warehouseinventory b ON a.ProductID=b.ProductID
WHERE ( a.ExpiryUnit=1 AND DATE_ADD(b.ProductionDate,INTERVAL a.ExpiryDate DAY)<=NOW() )
OR ( a.ExpiryUnit=2 AND DATE_ADD(b.ProductionDate,INTERVAL a.ExpiryDate MONTH)<=NOW())
OR ( a.ExpiryUnit=3 AND DATE_ADD(b.ProductionDate,INTERVAL a.ExpiryDate YEAR)<=NOW() )
AND a.CompanyID=@CompanyID
LIMIT 0,10;
SELECT FOUND_ROWS() AS TotalRecords;
SELECT SQL_CALC_FOUND_ROWS
a.ProductID,a.ProductName,a.ExpiryDate,a.ExpiryUnit,b.ProductionDate,b.WareHouseID,b.BinID,b.ShelfID
FROM product a
LEFT JOIN warehouseinventory b ON a.ProductID=b.ProductID
WHERE ( a.ExpiryUnit=1 AND DATE_ADD(b.ProductionDate,INTERVAL a.ExpiryDate DAY)<=NOW() )
OR ( a.ExpiryUnit=2 AND DATE_ADD(b.ProductionDate,INTERVAL a.ExpiryDate MONTH)<=NOW())
OR ( a.ExpiryUnit=3 AND DATE_ADD(b.ProductionDate,INTERVAL a.ExpiryDate YEAR)<=NOW() )
AND a.CompanyID=@CompanyID
LIMIT 0,10;
SELECT FOUND_ROWS() AS TotalRecords;
SELECT SQL_CALC_FOUND_ROWS
a.ProductID,a.ProductName,a.ExpiryDate,a.ExpiryUnit,b.ProductionDate,b.WareHouseID,b.BinID,b.ShelfID
FROM product a
LEFT JOIN warehouseinventory b ON a.ProductID=b.ProductID
WHERE ( a.ExpiryUnit=1 AND DATE_ADD(b.ProductionDate,INTERVAL a.ExpiryDate DAY)<=NOW() )
OR ( a.ExpiryUnit=2 AND DATE_ADD(b.ProductionDate,INTERVAL a.ExpiryDate MONTH)<=NOW())
OR ( a.ExpiryUnit=3 AND DATE_ADD(b.ProductionDate,INTERVAL a.ExpiryDate YEAR)<=NOW() )
AND a.CompanyID=@CompanyID
LIMIT 0,10;
SELECT FOUND_ROWS() AS TotalRecords;