gpt4 book ai didi

mysql - 如何在没有 key 的情况下连接表?

转载 作者:行者123 更新时间:2023-11-29 06:38:55 27 4
gpt4 key购买 nike

是否可以不用键连接表,

table 1
------------------------------
no code area
------------------------------
0122133666 MOB 012221
92444444 LOC 9244
0343443434 DDD 03434
0092635252 INF 92
------------------------------

table 2
------------------------------
code area name
------------------------------
MOB 012 mobile-1
MOB 0129 mobile-2
LOC 9244 local-1
LOC 9243 local-2
INF 92 country-1
DDD 034 local-2
DDD 0344 local-3
------------------------------

我想要这样的结果

------------------------------------
no code area name
------------------------------------
0122133666 MOB 012221 mobile-1
92444444 LOC 9244 local-1
0343443434 DDD 03434 local-2
0092635252 INF 92 country-1
--------------------------------------

看起来不可能那样做,谁能帮帮我?

最佳答案

这会给你想要的结果:

SELECT  T1.no,
T1.code,
T1.area,
T2.name
FROM Table1 T1
LEFT JOIN Table2 T2
ON T1.code = T2.code
AND T1.area LIKE CONCAT(T2.area,'%')

Here is a sqlfiddle有一个演示。

结果是:

╔═══════════╦══════╦════════╦═══════════╗
║ NO ║ CODE ║ AREA ║ NAME ║
╠═══════════╬══════╬════════╬═══════════╣
║ 122133666 ║ MOB ║ 012221 ║ mobile-1 ║
║ 92444444 ║ LOC ║ 9244 ║ local-1 ║
║ 343443434 ║ DDD ║ 03434 ║ local-2 ║
║ 92635252 ║ INF ║ 92 ║ country-1 ║
╚═══════════╩══════╩════════╩═══════════╝

关于mysql - 如何在没有 key 的情况下连接表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22797066/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com