Home  >  Article  >  Backend Development  >  用PHP与MYSQL按要求展示一个页面

用PHP与MYSQL按要求展示一个页面

WBOY
WBOYOriginal
2016-06-13 12:54:081081browse

用PHP与MYSQL按要求显示一个页面
我在MYSQL数据库中有两个表,A和B
A表有字段:a_id, a_name, a_description(a_id是唯一值,自动递增的)
B表有字段:b_id, a_id, b_name, b_description(b_id是唯一值,自动递增的,a_id是有重复的)

我在PHP文件中调用A表数据,并用while (!$a->EOF)...$a->MoveNext();将A表的所有行的记录都显示在我做的一个页面

现在,我需要在每行记录的旁边显示相对应的b_name字段值,
于是我用LEFT JOIN " . B . " b ON (b.a_id = a.a_id)

这样,b_name字段值是显示在旁边了,可是页面总行数一下子增多了,
因为原本总行数就是a_id的行数,现在有了重复的a_id行,这是由于a_id在B表中不是唯一值。

我只想按a_id的行数显示,就是一个a_id一行,在那行旁边显示对应的b_name,该如何做到呢?


------解决方案--------------------
select a.* , b.b_name from A left join B on A.a_id=b.a_id group by b.a_id
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn