条件sql查询结果范围

时间:2015-07-01 15:50:52

标签: mysql sql conditional-statements

我有一个查询,我希望根据特定字段显示不同的结果。例如,我正在使用

$result = $wpdb->get_results( "SELECT `b`.`company` AS `company`,`bp`.`material` AS `material`,
if(((`bp`.`cost` * 1.2) < `ls`.`maximumbid`),(ROUND(`bp`.`cost` * 1.2,2)),ROUND(`bp`.`cost`,2)) AS `newcost` 
from (((`windows_brands_products` `bp` left join `windows_brands` `b` on((`bp`.`brand_id` = `b`.`id`))) 
join `Windows_last_submissions` `ls`) join `windows_materials` `wm`) 
where ((`bp`.`width` = round(`ls`.`width`,0)) 
and (`bp`.`height` = round(`ls`.`height`,0))  
and (`bp`.`material` = `wm`.`name`) 
and (`bp`.`type` = `ls`.`type`) 
and if((`ls`.`minimumbid` <> '0.00'),(`bp`.`cost` between `ls`.`minimumbid` and `ls`.`maximumbid`),(`bp`.`cost` <= `ls`.`maximumbid`))) 
ORDER BY b.company ASC");

我想要的结果是行lstype =&#39;固定/图片&#39;,我想要bpwidth = round( lswidth, - 1)并且仍然可以用于任何其他ls.type

我应该从以下方面得到正确的结果:

select `b`.`company` AS `company`,`bp`.`material` AS `material`,if(((`bp`.`cost` * 1.2) < `ls`.`maximumbid`),round((`bp`.`cost` * 1.2),2),round(`bp`.`cost`,2)) AS `newcost` from (((`windows_brands_products` `bp` left join `windows_brands` `b` on((`bp`.`brand_id` = `b`.`id`))) join `Windows_last_submissions` `ls`) join `windows_materials` `wm`) 
where if((`ls`.`type` = 'Fixed/Picture'),(`bp`.`width` = round(`ls`.`width`,-2),(`bp`.`width` = round(`ls`.`width`,0)) and (`bp`.`height` = round(`ls`.`height`,0)) and (`bp`.`material` = `wm`.`name`) and (`bp`.`type` = `ls`.`type`) and if((`ls`.`minimumbid` <> '0.00'),(`bp`.`cost` between `ls`.`minimumbid` and `ls`.`maximumbid`),(`bp`.`cost` <= `ls`.`maximumbid`))) order by `b`.`company`

但是当我尝试纠正我没有成功的查询时,它一直给我语法错误。

如果类型是“固定/图片”

,那么这一点就是检索更广泛的结果。

谢谢你, 凯西

0 个答案:

没有答案