日期:2014-05-19  浏览次数:20524 次

没搞懂这个 sql 语句(俺没分了,真可怜)
select   a.day   'time '
from   `good_day_money`a,`card_sale_card`b
where   a.moneyOne   is   not   NULL   and   a.moneyTwo   is   not   NULL  

select   a.day   'time '
from   `good_day_money`a
where   a.moneyOne   is   not   NULL   and   a.moneyTwo   is   not   NULL  
返回的结果为什么不一样呢?
第一种返回的比如是:
2006-01-01
2006-03-12
2006-12-12
而第二种则返回:
2006-01-01
2006-01-01
2006-01-01
2006-03-12
2006-03-12
2006-03-12
2006-12-12
2006-12-12
2006-12-12
哪为肯告诉我是为什么呢?



------解决方案--------------------
第一个语句少连接条件,那两个表就是cross join,也就是产生笛卡儿乘积,只要card_sale_card有超过一条记录,就会有重复

------解决方案--------------------
纪录集颠倒了...

?
我的意思是,我想把所有表一里满足a.moneyOne is not NULL and a.moneyTwo is not NULL 条件的返回来,但如果我要是在加个关联条件and a.day =b.day如果这样的话返回的记录和我期望的少了,因为a中满足a.moneyOne is not NULL and a.moneyTwo is not NULL的可能是5条,而b中一共才4条记录,所以返回的就少了

肯定是要加的.
改改你的语句应该可以满足你的要求.
select a.day 'time '
from good_day_money a, card_sale_card b
where a.moneyOne is not NULL and a.moneyTwo is not NULL
and a.day *=b.day --这个是左连接. lz 去看看...left join