user2493164
user2493164

Reputation: 1331

mysql select columns and sum from other table

I have the following tables:

A: id, name, url
B: Aid, number

number can be any int (Aid is the id from table A)

I want to select the name, url from table A and the SUM(B.number) So the results will be something like:

name, url, SUM(B.number)
name, url, SUM(B.number)
name, url, SUM(B.number)
[..]

I need to join them somehow? How do i construct that query?

Upvotes: 1

Views: 2135

Answers (2)

Mateo Barahona
Mateo Barahona

Reputation: 1391

SELECT name, url, (SELECT SUM(number) FROM B WHERE B.Aid = A.id) As total
FROM A

Upvotes: 3

mic4ael
mic4ael

Reputation: 8290

SELECT A.name, A.url, SUM(B.number) 
FROM A
LEFT JOIN B ON A.id = B.Aid 
GROUP BY A.name, A.url

Please test it before cause I might have made a mistake since I got no MySQL DB available at the moment.

Upvotes: 2

Related Questions