[英]SQL JOIN WHERE IN STRING OF OTHER TABLE
I need to join two tables together where the seccond table has one of the first tables in a string. 我需要将两个表连接在一起,其中seccond表具有字符串中的第一个表之一。
eg 例如
Table1 has ID1, ID2, Lang_id, User, Text.
Table2 has ID_a, User, List_ID2.
Additionally, the List_ID2
is comma seperated, so I have to make sure I get it in all cases, so if it is the only number, or the at beginning, middle or end of the string. 另外,
List_ID2
是逗号分隔的,因此我必须确保在所有情况下都能得到它,因此,如果它是唯一的数字,或者是字符串的开头,中间或结尾。 BUT NOT truncate the values, ie (10 is not 100 - '10'0) 但是不要截断值,即(10不是100-'10'0)
The extra kicker is that Lang_id
is the language of the Place
and I also have to seperate that. 额外踢球是
Lang_id
是语言Place
,我也有单独的说。
I have the language seperated fine, I can't get the ID_a
into my results 我的语言分开很好,我无法将
ID_a
放入结果中
ie 即
TABLE 1
ID1 ID2 Lang_id User Text
1 2 1 bob Me
1 2 2 bob Mich
1 2 3 bob Mi
2 1 1 bob You
2 1 2 bob Du
2 1 3 bob usted
3 1 1 tim You
3 1 2 tim Dich
3 1 3 tim le
4 3 1 tim Hello
4 3 2 tim Hallo
4 3 3 tim ihola
TABLE 2
ID_a User List_ID2
100 bob 1, 2
200 tim 1, 3
RESULTS
ID_a ID1 ID2 English German Spanish
100 1 1 You Du usted
100 2 2 Me Mich Mi
200 3 1 You Dich le
200 4 3 Hello Hallo ihola
My statement looks a little like this: 我的陈述看起来像这样:
SELECT DISTINCT main.ID1, main.ID2, ID_a
(SELECT Text
FROM table1 AS a
WHERE lang_id = 1
AND main.ID1 = a.ID1
AND main.ID2 = a.ID2) AS English,
(SELECT Text
FROM table1 AS b
WHERE lang_id = 2
AND main.ID1 = b.ID1
AND main.ID2 = b.ID2) AS German,
(SELECT Text
FROM table1 AS c
WHERE lang_id = 3
AND main.ID1 = c.ID1
AND main.ID2 = c.ID2) AS Spanish,
FROM table1 AS main
LEFT OUTER JOIN table2 ON table2.User = main.User
AND (table2.List_ID2 LIKE STR(ID2)
OR table2.List_ID2 LIKE (ID2 + ',%')
OR table2.List_ID2 LIKE ('%,' + ID2 + ',%')
OR table2.List_ID2 LIKE ('%,' + ID2)
The first part is working fine (the language is seperated), but I can't get the ID from table2, I've tried a few differnt methods, and the above comes back without an error, but alot of NULL values. 第一部分工作正常(语言是单独的),但是我无法从table2获取ID,我尝试了几种不同的方法,并且上面的方法没有错误,但返回了很多NULL值。
Found the answer here: SQL STR() function equality 在这里找到答案: SQL STR()函数相等
I needed to specify the Length of the STR() 我需要指定STR()的长度
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.