如果一个值为NULL,则获取数据

时间:2013-11-20 07:08:07

标签: mysql sql

我遇到以下代码的问题,我的问题如下:

如果shop_shipping_rule_region_codecountry_iso,我需要能够选择NULL但我还需要获取$country_iso数据(如果有)

我的目标:

最终结果将包含:

Overnight UPS
NZ Snail Mail
Whole World (in this example)

DB:

`shop_shipping_rules` (`shop_shipping_rule_id`, `shop_shipping_rule_country_iso`, `shop_shipping_rule_region_code`, `shop_shipping_rule_name`, 
    `shop_shipping_rule_type_single`, `shop_shipping_rule_item_single`, `shop_shipping_rule_type_multi`, 
    `shop_shipping_rule_item_multiple`,`shop_shipping_rule_created`, `shop_shipping_rule_modified`, `website_id`)
     VALUES
    (41, 'NZ', 'ALL', 'Overnight UPS', 'single', 2.00, 'multi', 4.00, '2013-11-05 02:30:19', '2013-11-05 03:00:27', 64),
    (44, 'NZ', NULL, 'NZ Snail Mail', 'single', 25.00, 'multi', 35.00, '2013-11-14 03:57:06', NULL, 64),
    (45, NULL, 'ALL', 'Whole World', 'single', 90.00, 'multi', 150.00, '2013-11-14 05:05:53', NULL, 64),
    (46, NULL, 'EU', 'EU Ship', 'single', 28.00, 'multi', 35.00, '2013-11-15 01:04:01', NULL, 64);

1 个答案:

答案 0 :(得分:1)

if (isset($country_iso))
   $sql = "SELECT * FROM shop_shipping_rules WHERE country_iso IS NULL OR country_iso = '$country_iso' ";
else
    $sql = "SELECT * FROM shop_shipping_rules WHERE country_iso IS NULL";