现在有两张表,分别为
表A
id name
1 你好
2 世界
3 天下会
表B
id class
1 太行
2 宇宙
现在执行如下的语
select * from a left join b on a.id=b.id
结果
id name id class
1 你好 1 太行
2 世界 2 宇宙
3 天下会 NULL NULL
这个结果不难理解因为这是个左连接,自然也就会有后两个值都为null的情况出现
$query = mysql_query($sql);
$listarr = array();
while($row = mysql_fetch_array($query)){
var_dump($row['id']);
$listarr[$row['id']] = $row;
}
当用php 的 mysql_fetch_array()执行此函数时会得到如下的结果
string(1) "1"
string(1) "2"
NULL
第三行会出现 null 的情况 原因是第三的row['id']值是取第二个的id将之前有值 的那个给覆盖了,所以导致count($listarr)的值为二而数据库里面却又取出了三条数据,晕这么个东西让我搞了这么半天,已经是第二次了,现在把它放在这里。解决方法是在:表设计时应该尽可能让两张表的同一字段有所区别(这是个好主意)
另一种方法是 select *,a.id as aid from a left join b on a.id=b.id 在循环时取aid作为下标
表A
id name
1 你好
2 世界
3 天下会
表B
id class
1 太行
2 宇宙
现在执行如下的语
select * from a left join b on a.id=b.id
结果
id name id class
1 你好 1 太行
2 世界 2 宇宙
3 天下会 NULL NULL
这个结果不难理解因为这是个左连接,自然也就会有后两个值都为null的情况出现
$query = mysql_query($sql);
$listarr = array();
while($row = mysql_fetch_array($query)){
var_dump($row['id']);
$listarr[$row['id']] = $row;
}
当用php 的 mysql_fetch_array()执行此函数时会得到如下的结果
string(1) "1"
string(1) "2"
NULL
第三行会出现 null 的情况 原因是第三的row['id']值是取第二个的id将之前有值 的那个给覆盖了,所以导致count($listarr)的值为二而数据库里面却又取出了三条数据,晕这么个东西让我搞了这么半天,已经是第二次了,现在把它放在这里。解决方法是在:表设计时应该尽可能让两张表的同一字段有所区别(这是个好主意)
另一种方法是 select *,a.id as aid from a left join b on a.id=b.id 在循环时取aid作为下标