Sql 如何在一个表中选择另一个表中值不为null的列
我有两个SQL表(我使用的是SQLite) 如何获取表2中至少有一行代码table1不为null的所有table1内容Sql 如何在一个表中选择另一个表中值不为null的列,sql,sqlite,Sql,Sqlite,我有两个SQL表(我使用的是SQLite) 如何获取表2中至少有一行代码table1不为null的所有table1内容 select a.* from Table1 a where exists (select * from Table2 b where b.codeTable1 is not null) 或 不确定您的查询的确切目的 或 不确定查询的确切目的。我想您需要一个相关的子查询: select t1.* from Table1 t1 where exists (selec
select
a.*
from
Table1 a
where
exists (select * from Table2 b where b.codeTable1 is not null)
或
不确定您的查询的确切目的
或
不确定查询的确切目的。我想您需要一个相关的子查询:
select t1.*
from Table1 t1
where exists (select 1
from Table2 t2
where t2.code = t1.code and t2.codeTable1 is not null
);
这似乎是对您的需求的非常直接的翻译。我认为您需要一个相关的子查询:
select t1.*
from Table1 t1
where exists (select 1
from Table2 t2
where t2.code = t1.code and t2.codeTable1 is not null
);
这似乎是对您的需求的非常直接的翻译。一个简单的连接应该可以做到: 问题是:
select
*
from
table1
join table2 on table1.code = table2.table1_code
where
table1.code is not null
以下是完整的示例:
use example;
drop table if exists table1;
drop table if exists table2;
create table table1 (
code varchar(64)
);
create table table2 (
code varchar(64),
table1_code varchar(64) references table1(code)
);
insert into table1 values('CODE1');
insert into table1 values('CODE2');
insert into table1 values('CODE3');
insert into table1 values('CODE4');
insert into table1 values('CODE5');
insert into table1 values(null);
insert into table2 values('VAL1', 'CODE1');
insert into table2 values('VAL3', 'CODE3');
insert into table2 values('VAL5', 'CODE5');
insert into table2 values(null, null);
insert into table2 values(null, null);
insert into table2 values(null, null);
select
*
from
table1
join table2 on table1.code = table2.table1_code
where
table1.code is not null
+ --------- + --------- + ---------------- +
| code | code | table1_code |
+ --------- + --------- + ---------------- +
| CODE1 | VAL1 | CODE1 |
| CODE3 | VAL3 | CODE3 |
| CODE5 | VAL5 | CODE5 |
+ --------- + --------- + ---------------- +
3 rows
一个简单的连接应该可以做到这一点: 问题是:
select
*
from
table1
join table2 on table1.code = table2.table1_code
where
table1.code is not null
以下是完整的示例:
use example;
drop table if exists table1;
drop table if exists table2;
create table table1 (
code varchar(64)
);
create table table2 (
code varchar(64),
table1_code varchar(64) references table1(code)
);
insert into table1 values('CODE1');
insert into table1 values('CODE2');
insert into table1 values('CODE3');
insert into table1 values('CODE4');
insert into table1 values('CODE5');
insert into table1 values(null);
insert into table2 values('VAL1', 'CODE1');
insert into table2 values('VAL3', 'CODE3');
insert into table2 values('VAL5', 'CODE5');
insert into table2 values(null, null);
insert into table2 values(null, null);
insert into table2 values(null, null);
select
*
from
table1
join table2 on table1.code = table2.table1_code
where
table1.code is not null
+ --------- + --------- + ---------------- +
| code | code | table1_code |
+ --------- + --------- + ---------------- +
| CODE1 | VAL1 | CODE1 |
| CODE3 | VAL3 | CODE3 |
| CODE5 | VAL5 | CODE5 |
+ --------- + --------- + ---------------- +
3 rows