请教一个mysql查询问题,送上100分
表A 表B
uid mid cash uid mid cash number
1 1 100 1 1 101 100
2 2 105 2 1 100 100
3 3 98 3 2 105 200
4 4 55 4 2 105 300
5 5 60 5 3 60 300
6 6 70 6 3 98 300
uid 是自动增长字段 mid和cash 是关联字段
我想统计出 表B的求和数量就和下面这样,没匹配到表A就显示0,匹配通过mid和cash 两字段。请问SQL该怎么写?? 以表A为基础表
结果表
uid mid cash number
1 1 100 100
2 2 105 500
3 3 98 300
4 4 55 0
5 5 60 0
6 6 70 0
回复讨论(解决方案)
看这里
http://sqlfiddle.com/#!2/f5620/3
这个执行的结果还是不对啊,并没有求和
你太调皮,后来编辑过帖子了
看这个
http://sqlfiddle.com/#!2/9847b/12
create temporary table Aselect 1 as uid, 1 as mid, 100 as cashunion select 2, 2, 105union select 3, 3, 98union select 4, 4, 55union select 5, 5, 60union select 6, 6, 70;create temporary table Bselect 1 as uid, 1 as mid, 101 as cash, 100 as numberunion select 2, 1, 100, 100union select 3, 2, 105, 200union select 4, 2, 105, 300union select 5, 3, 60, 300union select 6, 3, 98, 300;select uid,mid, cash, (select sum(number) from B where mid=A.mid and cash=A.cash) from A
2 2 105 500
3 3 98 300
4 4 55
5 5 60
6 6 70
写测试数据花的时间比写查询指令的时间多十倍都不止
这个就是我想要的
select a.*,if(t.number is null,0,t.number) as number from a left join
(select b.mid,b.cash,sum(b.number) as number from b group by mid,cash) as t
on t.mid=a.mid and t.cash=a.cash
select a.uid, a.mid, a.cash, sum(b.number) from testa as a left join testb as b on a.mid=b.mid and a.cash = b.cash group by a.mid, a.cash
这个返回结果是
uid mid cash sum(b.number)
1 1 100 100
2 2 105 500
3 3 98 300
4 4 55
5 5 60
6 6 70

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.
