A、B两表,找出ID字段中,存在A表,但是不存在B表的数据。A表总共13w数据,去重后大约3W条数据,B表有2W条数据,且B表的ID字段有索引。
使用
not in
,容易理解,效率低
执行时间为:1.395秒
1 select distinct A.ID from A where A.ID not in (select ID from B)
使用 A left join B on... where B.ID is null 表示左连接之后在B.ID 字段为 null的记录 执行时间:0.739秒
1 select A.ID from A left join B on A.ID=B.ID where B.ID is null
逻辑相对复杂,但是速度最快 执行时间:0.570秒(感觉这种方式挺好)
select * from A where (select count(1) from B where A.ID=B.ID) = 0
A、B两表,找出ID字段中,存在A表,但是不存在B表的数据。A表总共13w数据,去重后大约3W条数据,B表有2W条数据,且B表的ID字段有索引。方法一使用 not in ,容易理解,效率低 执行时间为:1.395秒1 select distinct A.ID from A where A.ID not in (select ID from B)方法二使用 left join…on…...
sql的一个查询,情景:a表中存在的数据,且在b表中不存在 (not in,not exists
这里需要强调的是b表中关联字段的值是唯一的这种情况,并且b表尽量是列举类型的,意味着表比较小。
准备数据:
建两个类似表,test1,test2,只有id和val两个字段。
CREATE TABLE `test1` (
`id` int(8) unsigned NOT NULL AUTO_INCREMENT,
`val` varchar(20) DEFAULT NULL,
PRIMARY KEY (.
(转)A、B两表,找出ID字段中,存在A表,但是不存在B表的数据。A表总共13w数据,去重后大约3W条数据,B表有2W条数据,且B表的ID字段有索引。
使用 not in ,容易理解,效率低 ~执行时间为:1.395秒~
1 select distinct A.ID from A where A.ID not in (select ID from B)
现有a,b,c表需要连表查询,a表是主表接近百万数据,b,c表只有几百条,
select a.x, b.x, c.x from a left join b on a.key = b.key left join c on a.key = c.key where ...
select a.x, (select b.x from b where b.key = a.key) y, (se...
int a2 = any_cast(any_a1);
int* p_a2 = any_cast(&any_a1);
std::cout << "a2 = " << a2 <<" *p_a2="<<*p_a2<<std::endl;
any any_b1(12.35);
double b2 = any_cast(any_b1);
std::cout << "b2= " << b2 << std::endl;
any any_str(std::string("abc"));
std::string msg = any_cast(any_str);
std::cout << "msg= " << msg << std::endl;
std::vector values,new_values;
for(int idx=0;idx<10;idx++)
values.push_back(0.5+idx+10);
any any_vector(values);
new_values = any_cast<std::vector>(any_vector);
auto iter = new_values.begin();
while( new_values.end() != iter )
std::cout << *iter <<" ";
++ iter;
std::cout << std::endl;
sql多表连接查询inner join, left join , right join ,full join ,cross join
Posted on 2008-05-07 21:55 我开始懂了 阅读(21431) 评论(9) 编辑 收藏
inner join,full outer join,left join,right jion
内部连接 inner join 两表都满足的组合
full outer 全连 两表相同的组合在一起,A表有,B表没有的数据(显示为null),同样B表有
A表没有的显示为(null)
A表 left join B表 左连,以A表为基础,A表的全部数据,B表有的组合。没有的为null
A表 right join B表 右连,以B表为基础,B表的全部数据,A表的有的组合。没有的为null