select t1.name from a t1,b t2,c t3 where t1.id=t2.fk_id and t3.t_id=t2.id and t3.p_id not in(select id from d) group by t1.name;
create table A(id int primary key,name varchar(50));create table B(id int primary key,a_id int, foreign key(a_id) references A(id)); create table D(id int primary key,name varchar(50));create table C(id int primary key,b_id int, d_id int, foreign key(d_id) references D(id),foreign key(b_id) references B(id));insert into A values(1,'第一次'); insert into A values(2,'第二次');insert into B values(1,2); 两次任务都关联:第二次 insert into B values(2,2); insert into B values(3,1); insert into D values(1,'中山一号'); insert into D values(2,'中山二号');insert into C values(1,2,1); 任务详细都把D中的点都关联, insert into C values(2,1,2); insert into C values(3,3,1);我要得到结果: name ------ 第一次我要选出A中的name,它的C 表中的d_id没有全包含 D表的所有id.
explain select t.name from ( -> select a.id,count(c.d_id) n,a.name -> from A a,B b,C c -> where a.id=b.a_id and b.id = c.b_id group by a.id)t, -> -> (select count(*) m from D) n -> where t.n!=n.m; +----+-------------+------------+--------+---------------+---------+---------+-------------+------+---------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+---------------+---------+---------+-------------+------+---------------------------------+ | 1 | PRIMARY | <derived3> | system | NULL | NULL | NULL | NULL | 1 | | | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 2 | Using where | | 3 | DERIVED | D | index | NULL | PRIMARY | 4 | NULL | 2 | Using index | | 2 | DERIVED | c | ALL | b_id | NULL | NULL | NULL | 3 | Using temporary; Using filesort | | 2 | DERIVED | b | eq_ref | PRIMARY,a_id | PRIMARY | 4 | test.c.b_id | 1 | | | 2 | DERIVED | a | eq_ref | PRIMARY | PRIMARY | 4 | test.b.a_id | 1 | | +----+-------------+------------+--------+---------------+---------+---------+-------------+------+---------------------------------+ 6 rows in set (0.00 sec)这样的执行计划好不好哟? 有什么可以改进的?
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
where t1.id=t2.fk_id and t3.t_id=t2.id
and t3.p_id not in(select id from d)
group by t1.name;
create table D(id int primary key,name varchar(50));create table C(id int primary key,b_id int, d_id int, foreign key(d_id) references D(id),foreign key(b_id) references B(id));insert into A values(1,'第一次');
insert into A values(2,'第二次');insert into B values(1,2); 两次任务都关联:第二次
insert into B values(2,2);
insert into B values(3,1);
insert into D values(1,'中山一号');
insert into D values(2,'中山二号');insert into C values(1,2,1); 任务详细都把D中的点都关联,
insert into C values(2,1,2);
insert into C values(3,3,1);我要得到结果:
name
------
第一次我要选出A中的name,它的C 表中的d_id没有全包含 D表的所有id.
-> select a.id,count(c.d_id) n,a.name
-> from A a,B b,C c
-> where a.id=b.a_id and b.id = c.b_id group by a.id)t,
->
-> (select count(*) m from D) n
-> where t.n!=n.m;
+----+-------------+------------+--------+---------------+---------+---------+-------------+------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+---------+---------+-------------+------+---------------------------------+
| 1 | PRIMARY | <derived3> | system | NULL | NULL | NULL | NULL | 1 | |
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 2 | Using where |
| 3 | DERIVED | D | index | NULL | PRIMARY | 4 | NULL | 2 | Using index |
| 2 | DERIVED | c | ALL | b_id | NULL | NULL | NULL | 3 | Using temporary; Using filesort |
| 2 | DERIVED | b | eq_ref | PRIMARY,a_id | PRIMARY | 4 | test.c.b_id | 1 | |
| 2 | DERIVED | a | eq_ref | PRIMARY | PRIMARY | 4 | test.b.a_id | 1 | |
+----+-------------+------------+--------+---------------+---------+---------+-------------+------+---------------------------------+
6 rows in set (0.00 sec)这样的执行计划好不好哟?
有什么可以改进的?