SQL SELECTING * then select 来自以

SQL SELECTING * then select values from columns that end with

我有以下代码;

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

然后我需要 select 来自另一列的值 'code' 来自 'testtwo' 并且值以“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

对于值以 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'