简体   繁体   English

当列不正确匹配时,SQL查询如何从多个表返回数据

[英]How can an SQL query return data from multiple tables when column don't match properly

I've tried to find relevants informations but seems I can't figure this one by myself so I need assistance :) 我试图找到相关信息,但似乎我自己也找不到,所以我需要帮助:)

For exemple, Table1 : 例如,表1:

+-------+-------------+------+-----------+----------+
| ID    | SNA_Name    | Desc | Time      | Task_ID  |
+-------+-------------+------+-----------+----------+
| 741   | Temp        | NNE  | 2015-01-. | 1661     |
+-------+-------------+------+-----------+----------+

Table 2 : 表2:

+-------+-------------+
| ID    | Name        |
+-------+-------------+
| 741   | GRFDSD14    |
+-------+-------------+

Table 3 : 表3 :

+-------+-------------+------+-----------+-------------------------+
| xx    | USER        | xx   | xx        | Data                    |
+-------+-------------+------+-----------+-------------------------+
| xx    | Mylsef      | xx   | xx        | xxx.dd.1661 azerty      |
+-------+-------------+------+-----------+-------------------------+

I would like to have something like this at the end : 我希望最后有这样的东西:

(----------TABLE 1----------------------)(--Table 2---)(---TABLE 3---) 
+-------+-------------+------+-----------+-------------+-------------+
| ID    | SNA_Name    | Desc | Time      | Name        | USER        |
+-------+-------------+------+-----------+-------------+-------------+
| 741   | Temp        | NNE  |  2015-01-.| GRFDSD14    | Mylsef      |
+-------+-------------+------+-----------+-------------+-------------+

So far i've tried to match TABLE 1 and 2 like this using the same ID 741 到目前为止,我已经尝试使用相同的ID 741来匹配表1和2

SELECT a.ID, a.SNA_Name, a.Desc, a.Time, b.Name
FROM Table1 a
INNER JOIN Table2 b
on a.ID = b.ID

RESULT : 结果:

+-------+-------------+------+-----------+-------------+
| ID    | SNA_Name    | Desc | Time      | Name        |
+-------+-------------+------+-----------+-------------+
| 741   | Temp        | NNE  |  2015-01-.| GRFDSD14    |
+-------+-------------+------+-----------+-------------+

But to join the third Table I need to match with the Task_ID information : 1661 In the third Table, my ID is surrender by numbers so I can't use the symbol = And I want to print the Column USER in the same row where Data match with Task_ID 但是要加入第三个表,我需要与Task_ID信息匹配:1661在第三个表中,我的ID用数字表示,因此我不能使用符号=并且我想在数据所在的同一行中打印USER列与Task_ID匹配

Thanks a lot. 非常感谢。

[Edit -> Using SQL 2008 [编辑->使用SQL 2008

Try this for the join: 试试这个加入:

FROM Table1 
JOIN Table3 ON Table3.Data LIKE '%'+ CAST(Table1.Task_ID AS varchar(20)) + '%'

You don't mention what database you are using, so I can't give specifics, but I would try to create a calculated column using a regular expression that is able to compare for any of the situations you describe, thosae being... 您没有提及正在使用的数据库,因此我无法提供具体信息,但是我会尝试使用正则表达式创建一个计算列,该正则表达式能够针对您所描述的任何情况进行比较,例如……

sometimes the number has a period before it, sometimes it has a space before it, sometimes it has an apostrophe before it. 有时数字前面有一个句点,有时数字前有一个空格,有时数字前有撇号。

In the example below, the regular expression will check for any combination of the id with any of the above characters preceding it, and it also prevents mismatching from partial values - ie it will not match 56 and 4564. 在下面的示例中,正则表达式将检查id与前面的任何上述字符的任何组合,并且还防止与部分值不匹配-即它将不匹配56和4564。

SELECT
    *
FROM
   t1
LEFT JOIN 
    t2
ON 
    concat(t2.data, ' ') REGEXP concat('[\'. ]', t1.i2, ' ')

Some notes on the above - I concat a space onto the data column so that I always am guaranteed to have either the number followed by a space, or the number followed by some other text (like your "azerty"). 上面的一些注意事项-我在数据列上加了一个空格,以便始终保证我在数字后面加上一个空格,或者在数字后面加上一些其他文本(例如您的“ azerty”)。 I concat a space on the end of the id so that it will prevent matches where the same id is embedded in another value. 我在ID的末尾连接一个空格,以防止在另一个值中嵌入相同ID的情况下进行匹配。

I hope this is helpful for someone. 我希望这对某人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM