php – 从具有多个要求的两个表中选择值,但仅使用其中一个

我想根据条件加入两个表.

表A:

+--------+-------------+------+
| prefix | Destination | rate |
+----------------------+------+
| 56     |  Monn       |  25  |
| 5602   |  Monn M1    |  23  |
| 5604   |  Monn M3    |  44  |
| 5607   |  Monn M1    |  23  |
| 5625   |  Monn M2    |  22  |
| 23     |  Xpia       |  0.3 |
| 238    |  Xpia F3    |  0.9 |
+--------+-------------+------+

表B:

+--------+-------------+------+
| prefix | Destination | rate |
+----------------------+------+
| 56     |  Monn       |  75  |
| 560    |  Monn M1x   |  49  |
| 5607   |  Monn M1    |  03  |
| 56254  |  Monn M2    |  9.5 |
| 23     |  Xpia       |  1.3 |
| 2301   |  Xpia T1    |  2.4 |
| 2302   |  Xpia T2    |  3.5 |
| 2381   |  Xpia F     |  8.9 |
+--------+-------------+------+

期望的输出:

表C:

+--------+-------------+------+
| prefix | Destination | rate |
+----------------------+------+
| 56     |  Monn       |  75  |
| 5602   |  Monn M1    |  49  |
| 5604   |  Monn M3    |  49  |
| 5607   |  Monn M1    |  03  |
| 5625   |  Monn M2    |  9.5 |
| 23     |  Xpia       |  1.3 |
| 238    |  Xpia F3    |  8.9 |
+--------+-------------+------+

请注意以下条件:

>我只想使用表A的Prefix和Destination列.
>我想只使用表B的费率栏.
>如果表A的前缀=表B的前缀,则复制速率
>如果在表B中未找到表A的前缀,则表B中以表A的前缀开头的前缀的复制速率(仅返回最长字符串的值).
>如果表A的前缀不在表B中,则表B中前缀的复制速率,其中表A的前缀是以表B中的前缀开头.(仅返回最长字符串的值)

如果满足条件4和5,则仅返回最长前缀的速率.

我有这个查询,但它无法正常工作.我意识到我需要一个更复杂的查询.

INSERT INTO Table C(prefix,destination, rate) 
       SELECT Table A.prefix, Table A.destination, Table B.rate 
       FROM Table A, Table B 
       WHERE Table B.prefix= SUBSTRING(Table A.prefix, 1, length(Table B.prefix))
最佳答案
免责声明:以下答案很长,所以我首先会引起你的注意,说最后的查询会产生符合你想要的结果.这是SQL Fiddle.

那么,在我看来,解决这个问题的最佳方法是一次一个要求.

I want to use only Prefix and Destination columns of table A.

这只是一个简单的选择语句:

SELECT a.prefix, a.destination
FROM tableA a;

I want to use only rate column of table B.

我们也可以轻松添加它.请注意,我现在所拥有的只是创建一个笛卡尔积,但随着我们添加更多要求,它将会理顺:

SELECT a.prefix, a.destination, b.rate
FROM tableA a, tableB b;

If Prefix of table A = Prefix of table B, then copy rate.

我更改了上面的内容,在select子句中使用相关子查询,如果它具有与tableA匹配的前缀,则会提取速率.对于任何不匹配的值,这将为null(暂时):

SELECT a.prefix, a.destination, 
   (SELECT b.rate FROM tableB b WHERE b.prefix = a.prefix) AS rate
FROM tableA a;

If prefix of table A is not found in table B, then copy rate of prefix in table B that starts with prefix of table A (return only the longest string’s value).

为此,我离开了一秒钟,写了一个查询,从B获取前缀从表A开始的B的速率,忘记了先前的条件.我像你一样使用了substring函数,但是按照长度的降序排序得到最大的一个:

SELECT a.prefix, a.destination, 
   (SELECT b.rate FROM tableB b WHERE b.prefix = 
   SUBSTRING(a.prefix, 1, LENGTH(b.prefix)) ORDER BY LENGTH(b.prefix) DESC LIMIT 1) AS rate
FROM tableA a;

现在,您可以获取该选择查询及其上方的查询,并使用COALESCE函数获取第一个非空值.因此,返回空值的步骤3中的值将替换为步骤4的值(如果仍有非空值):

SELECT a.prefix, a.destination, COALESCE(
  (SELECT b.rate FROM tableB b WHERE b.prefix = a.prefix),
  (SELECT b.rate FROM tableB b WHERE b.prefix = SUBSTRING(a.prefix, 1, LENGTH(b.prefix)) ORDER BY LENGTH(b.prefix) DESC LIMIT 1)) AS rate  
FROM tableA a;

If prefix of table A is not in table B then copy rate of prefix in table B where prefix of table A starts with a prefix in table B. (return only longest string’s value).

好吧,除了操作子查询以检查相反的表之外,我们可以做与上一次相同的操作.此外,COALESCE函数没有参数限制,因此我们可以将其添加为第三个参数.如果第一个返回null,它将尝试第二个.如果返回null,它将尝试第三个.这是最后的查询:

SELECT a.prefix, a.destination, COALESCE(
  (SELECT b.rate FROM tableB b WHERE b.prefix = a.prefix),
  (SELECT b.rate FROM tableB b WHERE b.prefix = SUBSTRING(a.prefix, 1, LENGTH(b.prefix)) ORDER BY LENGTH(b.prefix) DESC LIMIT 1),
  (SELECT b.rate FROM tableB b WHERE a.prefix = SUBSTRING(b.prefix, 1, LENGTH(a.prefix)) ORDER BY LENGTH(a.prefix) DESC LIMIT 1)) AS rate
FROM tableA a;

当我将结果与你的结果进行比较时,我注意到上面没有考虑到4和5都满足,在这种情况下我们想要采用最长的前缀.虽然可能有更简洁的方法来编写它,但我只写了以下case语句:

SELECT a.prefix, a.destination,
  CASE WHEN 
    (SELECT b.rate FROM tableB b WHERE b.prefix = a.prefix) IS NOT NULL
  THEN
    (SELECT b.rate FROM tableB b WHERE b.prefix = a.prefix)
  ELSE
    CASE WHEN
      ((SELECT b.rate FROM tableB b WHERE b.prefix = SUBSTRING(a.prefix, 1, LENGTH(b.prefix)) ORDER BY LENGTH(b.prefix) DESC LIMIT 1) IS NOT NULL)
      AND
      ((SELECT b.rate FROM tableB b WHERE a.prefix = SUBSTRING(b.prefix, 1, LENGTH(a.prefix)) ORDER BY LENGTH(a.prefix) DESC LIMIT 1) IS NOT NULL)
    THEN
      CASE WHEN 
        (SELECT LENGTH(b.prefix) FROM tableB b WHERE b.prefix = SUBSTRING(a.prefix, 1, LENGTH(b.prefix)) ORDER BY LENGTH(b.prefix) DESC LIMIT 1)
        >
        (SELECT LENGTH(a.prefix) FROM tableB b WHERE a.prefix = SUBSTRING(b.prefix, 1, LENGTH(a.prefix)) ORDER BY LENGTH(a.prefix) DESC LIMIT 1)
      THEN
        (SELECT b.rate FROM tableB b WHERE b.prefix = SUBSTRING(a.prefix, 1, LENGTH(b.prefix)) ORDER BY LENGTH(b.prefix) DESC LIMIT 1)
      ELSE
        (SELECT b.rate FROM tableB b WHERE a.prefix = SUBSTRING(b.prefix, 1, LENGTH(a.prefix)) ORDER BY LENGTH(a.prefix) DESC LIMIT 1)
      END
    ELSE
      COALESCE(
        (SELECT b.rate FROM tableB b WHERE b.prefix = SUBSTRING(a.prefix, 1, LENGTH(b.prefix)) ORDER BY LENGTH(b.prefix) DESC LIMIT 1),
        (SELECT b.rate FROM tableB b WHERE a.prefix = SUBSTRING(b.prefix, 1, LENGTH(a.prefix)) ORDER BY LENGTH(a.prefix) DESC LIMIT 1))
    END
  END AS rate
FROM tableA a;

它背后的逻辑有点像:

>检查是否符合条件3.

>如果是,请使用该值.

>检查是否满足条件4和5.

>如果是,请检查条件4中的前缀是否更长.

>如果是,请使用它.如果不是,请使用条件5中的前缀.

>如果不是,请从条件4和5中选择第一个非空条件.

转载注明原文:php – 从具有多个要求的两个表中选择值,但仅使用其中一个 - 代码日志