select t1.a, t2.b
from table1 t1, table2 t2
where t1.id=t2.id
and t1.id='12345';
这样仅返回符合条件的唯一一条记录,但若使用:
select t1.a, t2.b
from table1 t1, table2 t2, table2 t3
where t1.id=t2.id
and t1.id='12345';
则会返回N条一样的数据。这里虽然FROM中用了table2 t3,但查询条件包括SELECT中并没有用到t3的任何字段,为什么会出现这种错误?谢谢!
from table1 t1, table2 t2
where t1.id=t2.id
and t1.id='12345';
这样仅返回符合条件的唯一一条记录,但若使用:
select t1.a, t2.b
from table1 t1, table2 t2, table2 t3
where t1.id=t2.id
and t1.id='12345';
则会返回N条一样的数据。这里虽然FROM中用了table2 t3,但查询条件包括SELECT中并没有用到t3的任何字段,为什么会出现这种错误?谢谢!
from table1 t1, table2 t2
where t1.id=t2.id
and t1.id='12345';首先这个查询会先执行from table1 t1, table2 t2 然后再执行where 最后从结果中选出你需要的字段
而from table1 t1, table2 t2 执行的是全连接 ,也就是笛卡尔积 。 而如果执行select t1.a, t2.b
from table1 t1, table2 t2, table2 t3
where t1.id=t2.id
and t1.id='12345';执行的三个表的全连接,同两个表的全连接是完全不同的,这两种情况下的基本数据是不一样的,前者会更多。这样执行同样的where后,记录数肯定是不一定的
则from t1, t2的结果是m * n条记录
from t1, t2, t3的结果是m * n * k条记录。
然后T1和T2表的数据结果,和T3表产生笛卡尔积。所以很多重复的数据。