mysql:比较两列

时间:2015-07-22 10:36:14

标签: mysql regex select join

我的桌子及其布局:

mysql> select * FROM xt_shipping_zones;
+---------+-------------+---------------------------------------------------------------------------+
| zone_id | zone_name   | zone_countries                                                            |
+---------+-------------+---------------------------------------------------------------------------+
|       5 | ZONE1       | AT,BE,BG,DK,FI,FR,GR,IE,IT,LV,LT,LU,MC,NL,PL,PT,RO,SM,SE,SK,SI,ES,HU,GB   |
|       6 | Deutschland | DE                                                                        |
|       8 | ZONE2Brutto | AD,NO,VA                                                                  |
|       9 | ZONE2NETTO  | CH,LI                                                                     |
+---------+-------------+---------------------------------------------------------------------------+


mysql> select * FROM xt_shipping_cost WHERE shipping_geo_zone = 99995 LIMIT 5;
+------------------+-------------+-------------------+-----------------------+--------------------------+------------------------+----------------+------------------+
| shipping_cost_id | shipping_id | shipping_geo_zone | shipping_country_code | shipping_type_value_from | shipping_type_value_to | shipping_price | shipping_allowed |
+------------------+-------------+-------------------+-----------------------+--------------------------+------------------------+----------------+------------------+
|              269 |          34 |             99995 |                       |                     0.31 |                  17.99 |        17.0000 |                1 |
|              270 |          34 |             99995 |                       |                    17.99 |                  35.99 |        34.0000 |                1 |
|              271 |          34 |             99995 |                       |                    35.99 |                  53.99 |        51.0000 |                1 |
|              272 |          34 |             99995 |                       |                    53.99 |                  71.99 |        68.0000 |                1 |
|              273 |          34 |             99995 |                       |                    71.99 |                  89.99 |        85.0000 |                1 |
+------------------+-------------+-------------------+-----------------------+--------------------------+------------------------+----------------+------------------+


mysql> SELECT * FROM geoip WHERE 92569600 BETWEEN start AND end;
+----------+----------+---------+-----+
| start    | end      | country | id  |
+----------+----------+---------+-----+
| 92569600 | 92585983 | AT      | 895 |
+----------+----------+---------+-----+

我的查询:

SELECT
  xt_shipping_cost.shipping_type_value_from,
  xt_shipping_cost.shipping_type_value_to,
  xt_shipping_cost.shipping_price,
  geoip.country
FROM xt_shipping_cost
  INNER JOIN xt_shipping_zones
    ON xt_shipping_cost.shipping_geo_zone = xt_shipping_zones.zone_id + 99990
  INNER JOIN geoip
    ON geoip.country REGEXP xt_shipping_zones.zone_countries
WHERE 34664448 BETWEEN geoip.start AND geoip.end

我的问题:

如果 xt_shipping_zones.zone_countries 中只有 ONE 条目,则查询正在运行。如果有多个(使用逗号分隔的条目)我不能在该行上获得匹配。

手动完成:

mysql> SELECT *  FROM  `xt_shipping_zones`  WHERE  `zone_countries`  REGEXP  'AT';
+---------+-----------+---------------------------------------------------------------------------+
| zone_id | zone_name | zone_countries                                                            |
+---------+-----------+---------------------------------------------------------------------------+
|       5 | ZONE1     | AT,BE,BG,DK,FI,FR,GR,IE,IT,LV,LT,LU,MC,NL,PL,PT,RO,SM,SE,SK,SI,ES,HU,GB   |
+---------+-----------+---------------------------------------------------------------------------+

SQLFiddle:http://sqlfiddle.com/#!9/68f8d0/1

我希望我没有做太多让我的问题清楚。

谢谢

1 个答案:

答案 0 :(得分:0)

我认为您可以使用find_in_set()

SELECT
  xt_shipping_cost.shipping_type_value_from,
  xt_shipping_cost.shipping_type_value_to,
  xt_shipping_cost.shipping_price,
  geoip.country
FROM xt_shipping_cost
  INNER JOIN xt_shipping_zones
    ON xt_shipping_cost.shipping_geo_zone = xt_shipping_zones.zone_id + 99990
  INNER JOIN geoip
    ON find_in_set(geoip.country, xt_shipping_zones.zone_countries)
WHERE 34664448 BETWEEN geoip.start AND geoip.end

将值存储为csv不是一个好主意。这是非常糟糕的数据库设计。