SQL SELECTING *,然后从以下列结尾的列中选择值

时间:2019-02-22 08:13:05

标签: sql join sql-like

我有以下代码;

Select * from test left join testtwo on test.testid = testtwo.id

然后我需要从“ testtwo”的另一列“ code”中选择值,并且这些值以“ 100”('%100')结尾

我尝试了以下代码,但是没有用:

Select * from test left join testtwo on test.testid = testtwo.id
union
SELECT * FROM testtwo
WHERE code LIKE '100%';

id    testid   code
1     1       0001100
2     2       0002100
3     3       0003100
4     4       0004100

1 个答案:

答案 0 :(得分:1)

对于值以100结尾的字符串列,应使用

 WHERE code LIKE '%100'; 

查看您的示例,您可以使用

Select * 
from test 
INNER  join testtwo on test.testid = testtwo.id
WHERE code LIKE '%100';

如果您还需要'%400',则可以使用OR条件

Select * 
from test 
INNER  join testtwo on test.testid = testtwo.id
WHERE code LIKE '%100' 
OR code LIKE '%400' ;

或使用联合

Select * 
from test 
INNER  join testtwo on test.testid = testtwo.id
WHERE code LIKE '%100' 
union 
Select * 
from test 
INNER  join testtwo on test.testid = testtwo.id
WHERE code LIKE '%400'