0
SELECT nl.legal_name, nl.city, c.description 'Country', it.lei, sec.sym
FROM name_loc             nl
INNER JOIN ident_tbl_tmp  it ON nl.fk_ident_id = it.id 
INNER JOIN sym_exch_cnty sec ON it.fk_sec_id   = sec.id
INNER JOIN countries       c ON nl.fk_cnty_id  = c.id
WHERE legal_name REGEXP '^For'
limit 100;

使用上述查询将返回 500+ 行数据。部分输出是:

输出:

+------------------------------------------+--------------+----------------+----------------------+--------------+
| legal_name                               | city         | Country        | lei                  | sym          |
+------------------------------------------+--------------+----------------+----------------------+--------------+
| FOREFRONT GROUP LTD HKD0.01(SUB          | PENDING      | HONG KONG      | NA                   | 2903.HK      |
| FOREFRONT HOLDINGS                       | PENDING      | UNITED STATES  | NA                   | FFHN         |
| FOREIGN & COL INV TR                     | PENDING      | UNITED STATES  | NA                   | FLIVF        |
| Foreign & Colonial Investment Trust      | PENDING      | NEW ZEALAND    | NA                   | FCT.NZ       |
| Foreign & Colonial Investment Trust      | PENDING      | UNITED KINGDOM | NA                   | FRCL.L       |
| Foreign & Colonial Investment Trust PLC  | London       | UNITED KINGDOM | 8VHDVYVI7W11JH2PAC61 | NA           |
| Foreland                                 | PENDING      | SINGAPORE      | NA                   | E1:B0I.SI    |
| Foreland                                 | PENDING      | SINGAPORE      | NA                   | E2:B0I.SI    |

我需要一个查询,当前“n”个字符匹配并且国家/地区相同时,该查询将返回结果。

这将是匹配前 7 个字符的正确结果:

+------------------------------------------+--------------+----------------+----------------------+--------------+
| legal_name                               | city         | Country        | lei                  | sym          |
+------------------------------------------+--------------+----------------+----------------------+--------------+
| Foreign & Colonial Investment Trust      | PENDING      | UNITED KINGDOM | NA                   | FRCL.L       |
| Foreign & Colonial Investment Trust PLC  | London       | UNITED KINGDOM | 8VHDVYVI7W11JH2PAC61 | NA           |
| Foreland                                 | PENDING      | SINGAPORE      | NA                   | E1:B0I.SI    |
| Foreland                                 | PENDING      | SINGAPORE      | NA                   | E2:B0I.SI    |

这将是匹配前 14 个字符的正确结果:

+------------------------------------------+--------------+----------------+----------------------+--------------+
| legal_name                               | city         | Country        | lei                  | sym          |
+------------------------------------------+--------------+----------------+----------------------+--------------+
| Foreign & Colonial Investment Trust      | PENDING      | UNITED KINGDOM | NA                   | FRCL.L       |
| Foreign & Colonial Investment Trust PLC  | London       | UNITED KINGDOM | 8VHDVYVI7W11JH2PAC61 | NA           |

我尝试了各种子查询,但没有运气。我认为我可能需要一个函数或过程,但我不确定。

4

1 回答 1

1

你可以简单地GROUP BY Country, LEFT(legal_name, 7). 这将确保您只获得国家和名称前缀的每个组合的单行输出。您对将是哪一行没有影响。COUNT(*) AS number_of_duplicates如果您想跟踪原始行数,您甚至可以添加一列。

于 2013-01-14T06:33:17.960 回答