php+mysql select a.*,b.* from tb1 as a,tb2 as b的查询问题

yalan 2014-03-25 01:53:21
假设有两各表分别为tb1和tb2,字段如下
tb1的字段:id,name
tb2的字段:id,tb1id,name

现在用sql语句:select a.*,b.* from tb1 as a,tb2 as b where a.id=b.tb1id
来检索符合条件的数据
在mysql的命令行中能显示,但是怎么用php语法来显示获取的数据呢?

比如我想要a.id,b.name,直接用php的rs["a.id"]是不对的。不知道这个语法怎么写~~~~
...全文
448 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2014-03-25
  • 打赏
  • 举报
回复
mysql_fetch_assoc 返回的是关联数组,很自然关联键是唯一的 你可以用 mysql_fetch_row 返回下标数组,那就不会被覆盖了
嘻哈大咖秀 2014-03-25
  • 打赏
  • 举报
回复
引用 6 楼 yalan 的回复:
如果两个表重复的字段非常多,并且干脆字段完全相同,那么非要这么一个字段一个字段的写? 我模模糊糊记得ASP是可以区分,忘了语法了,好像是类似rs("a.id")什么的,但是PHP测试不通
只能一个一个的写 要不区别不了 两个表也不会有太多相同的 要不分两个表干嘛
yalan 2014-03-25
  • 打赏
  • 举报
回复
引用 4 楼 microlab2009 的回复:
select a.id as aid,b.id as bid,b.name from tb1 as a,tb2 as b where a.id=b.tb1id 取的时候aid表示tb1里的id,bid表示tb2表的id
如果两个表重复的字段非常多,并且干脆字段完全相同,那么非要这么一个字段一个字段的写? 我模模糊糊记得ASP是可以区分,忘了语法了,好像是类似rs("a.id")什么的,但是PHP测试不通
yalan 2014-03-25
  • 打赏
  • 举报
回复
引用 1 楼 xuzuning 的回复:
$rs = mysql_query('select a.*,b.* from tb1 as a,tb2 as b where a.id=b.tb1id');
while($row = mysql_fetch_assoc($rs)) {
  $res[] = $row;
}
print_r($res);
这个我也知道,能输出数组,但是不能达到我的要求。 比如我想要一个表: 'select a.*,b.* from tb1 as a,tb2 as b where a.id=b.tb1id and b.name=''动画片'; tb1 tb2 id name id tb1id name 要怎么用php语法来区分a.id a.name和b.id b.name呢?
嘻哈大咖秀 2014-03-25
  • 打赏
  • 举报
回复
select a.id as aid,b.id as bid,b.name from tb1 as a,tb2 as b where a.id=b.tb1id 取的时候aid表示tb1里的id,bid表示tb2表的id
yalan 2014-03-25
  • 打赏
  • 举报
回复
引用 2 楼 microlab2009 的回复:
select a.id,b.name from tb1 as a,tb2 as b where a.id=b.tb1id
这个我知道肯定能行,但是如果仪表有很多列要显示呢?并且要同时显示tb1和tb2两个表的ID,怎么在php语法中区分a.id和b.id呢? 我隐隐约约记得老早写asp的时候可以在脚本中写的~~~
嘻哈大咖秀 2014-03-25
  • 打赏
  • 举报
回复
select a.id,b.name from tb1 as a,tb2 as b where a.id=b.tb1id
xuzuning 2014-03-25
  • 打赏
  • 举报
回复
$rs = mysql_query('select a.*,b.* from tb1 as a,tb2 as b where a.id=b.tb1id');
while($row = mysql_fetch_assoc($rs)) {
  $res[] = $row;
}
print_r($res);

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧