-3

表格1

|-----|---------------|---------------------|
| id  | name          |         pid         |
|-----|---------------|---------------------|
| 1   |  ram          |          2          |
| 2   |  rani         |          1          |
| 3   |   ram         |          3          |
|-------------------------------------------|

表 2

|-----|---------------|---------------------|
| pid | name          |         price       |
|-----|---------------|---------------------|
| 1   |  soap         |          2000       |
| 2   |  towel        |          1333       |
| 3   |  bed          |          3000       |
|-------------------------------------------|

我需要下表给出的结果

|-----|---------------|---------------------|
| id  | name          |         price       |
|-----|---------------|---------------------|
| 1   |  ram          |          4333       |
| 2   |  rani         |          2000       |
|-------------------------------------------|
4

1 回答 1

2
SELECT  MIN(a.ID) ID, a.name, SUM(b.Price) Price
FROM    table1 a
        INNER JOIN table2 b
            ON a.PID = b.PID
GROUP   BY a.Name

输出

╔════╦══════╦═══════╗
║ ID ║ NAME ║ PRICE ║
╠════╬══════╬═══════╣
║  1 ║ ram  ║  4333 ║
║  2 ║ rani ║  2000 ║
╚════╩══════╩═══════╝
于 2013-04-10T07:50:52.207 回答