Home > Backend Development > PHP Tutorial > 急高分请问MySQL查询有关问题

急高分请问MySQL查询有关问题

WBOY
Release: 2016-06-13 13:35:17
Original
900 people have browsed it

急!!!高分请教MySQL查询问题
现有表A和B
A表有一列bid允许为空,对应B表主键自增列
如果建视图的话,视图只会显示bid不为空的数据
A表bid为空的数据则显示不出
现在我想既有对应关系,又能显示A表bid为空的数据
只用一条语句,有办法实现么?该怎么写

SQL code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
例如:
表A
-----------------
id    bid    name
1     2      abc
2            def
3     1      ghi

表B
-----------------
bid   other
1     aaa
2     bbb

如果A的bid关联了B的bid
结果则为:
-----------------
id    bid    name   other
1     2      abc     aaa
3     1      ghi     bbb

以上
A表bid为空的列就没有了

现在我想要的结果则是:
-----------------
id    bid    name   other
1     2      abc     aaa
2            def
3     1      ghi     bbb

Copy after login


高分请教

------解决方案--------------------
去搜一下join left和join right我相信能够解决你的问题
------解决方案--------------------
select a.id,a.bid,a.name, b.other from A a left join B b on a.bid=b.bid;
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template