(Sorry for bad title and context, I am not native English speaker)
Imagine this is my database's tables:
Table #1: Parent
╔══════════╦═════════════════╦═══════╗
║Child_id ║Primary key - AI ║ int ║
╠══════════╬═════════════════╬═══════╣
║parent_id ║Index-Forign_key ║ int ║
║══════════╬═════════════════╬═══════╣
║title ║ - ║varchar║
╚══════════╩═════════════════╩═══════╝
Table #2: Paid
╔══════════╦═════════════════╦═══════╗
║Paid_id ║Primary key - AI ║ int ║
╠══════════╬═════════════════╬═══════╣
║child_id ║Index-Forign_key ║ int ║
║══════════╬═════════════════╬═══════╣
║paid_price║ - ║int ║
╚══════════╩═════════════════╩═══════╝
Now I want to have a query, that return sum of paid_price
, by parent_id
I mean the query get all child_id
s from parent
table, then by each child_id
get sum of paid_price
from paid
table.
Imagine these are my tables' data:
Table: parent (data)
╔══════════╦══════════╦═══════╗
║ Child_id ║parent_id ║ title ║
╠══════════╬══════════╬═══════╣
║ 1 ║25 ║bla-bla║
║══════════╬══════════╬═══════╣
║ 2 ║25 ║bla-bla║
║══════════╬══════════╬═══════╣
║ 3 ║5 ║bla-bla║
║══════════╬══════════╬═══════╣
║ 4 ║25 ║bla-bla║
╚══════════╩══════════╩═══════╝
Table: paid (data)
╔══════════╦══════════╦════════════╗
║ Paid_id ║Child_id ║ paid_price ║
╠══════════╬══════════╬════════════╣
║ 1 ║1 ║1000000 ║
║══════════╬══════════╬════════════╣
║ 2 ║2 ║2500000 ║
║══════════╬══════════╬════════════╣
║ 3 ║1 ║3506000 ║
║══════════╬══════════╬════════════╣
║ 4 ║1 ║1700000 ║
╚══════════╩══════════╩════════════╝
I hope I could describe it well that what I need
*Note: This is not my database structure. I just wanted to have query. that was harder to write my real database's tables.