我对此SQL语句有疑问:
$sql="SELECT b.FrameNumber, b.BikeCode, IF(b.ProductID = 0, p.FrameNumber)
AS FrameNumber
FROM BikeStock b LEFT JOIN Purchase p ON b.FrameNumber = p.FrameNumber
WHERE b.bikecode = '$bikecode'";
我希望SQL语句以这种方式工作:
Select FrameNumber & BikeCode from BikeStock
THEN IF ProductID(BikeStock) is equal to 0
then it will grab the framenumber from the purchase table where bikecode is equal to $bikecode
有任何想法吗?
提前致谢。
最佳答案
MySQL IF
语句具有以下语法:IF(condition, expression if true, expression if false)
。
据我从您的问题中看到的,您实际上是在寻找WHERE
子句(请记住,您在WHERE
中可以有多个条件–使用布尔运算符将它们组合起来):
SELECT b.FrameNumber, b.BikeCode, p.FrameNumber
FROM BikeStock b
LEFT JOIN Purchase p ON b.FrameNumber = p.FrameNumber
WHERE b.bikecode = :bikecode AND b.ProductID = 0
关于php - MySQL- IF语句错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13790702/