是否有人知道是否可以在一个查询中执行以下操作而不是执行数字?
$select1 = "SELECT service FROM UPSServices WHERE code = '$serviceCode' AND ship_from_code = '$shipFrom'";
$result = mssql_query($select1);
//print_r($result);
if(mssql_num_rows($result) == 0) {
$select2 = "SELECT service FROM UPSServices WHERE code = '$serviceCode' AND ship_from_code IS NULL";
$result = mssql_query($select2);
}
while ($service = mssql_fetch_array($result)) {
return $service['0'];
}
答案 0 :(得分:1)
您正在使用SQL Server,因此您可以执行此操作。它比你想象的要复杂一些。以下版本计算有效值的数量。如果它大于0,则过滤掉NULL。
select service
from (SELECT service,
count(ship_from_code) over () as NumValues
FROM UPSServices
WHERE code = '$serviceCode' AND (ship_from_code = '$shipFrom' OR ship_from_code IS NULL)
) t
where (NumValues > 0 and service is not NULL) or (NumValues = 0 and service is NULL)
limit 1
答案 1 :(得分:0)
如果指定的shipCode不存在,我认为你需要使用NULL shipCode的值
SELECT service FROM UPSServices WHERE
code = '$serviceCode' AND (ship_from_code = '$shipFrom' OR ship_from_code IS NULL)
ORDER BY ship_from_code"
如果您的船舶代码存在,那么请按顺序确保其位于顶部