Home > Backend Development > PHP Tutorial > 问个连表的MYSQL查询语句

问个连表的MYSQL查询语句

WBOY
Release: 2016-06-23 13:42:18
Original
1116 people have browsed it


假如
表1
id    name 
1       A
2       B

表2
id    money
1         10
1          15
2          20
1          10

如何用左链表的方式,获得如下结果呢

1     A     40
2     B     20


回复讨论(解决方案)

select a.*, sum(money) as money from 表1 a left join 表2 b on a.id=b.id

1     A     45
2     B     20

select a.*, sum(money) as money from 表1 a left join 表2 b on a.id=b.id

1     A     45
2     B     20



这种写法出来的是一条记录
1 A 55.00

select a.*, sum(money) as money from 表1 a left join 表2 b on a.id=b.id

1     A     45
2     B     20



少了 group by 
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