-4

我通过比较需要结果的 2 个表给出了 2 个表。结果表在最后一个表中给出。表一

|-----|---------------|---------------------|
| id  | name          |         pid         |
|-----|---------------|---------------------|
| 1   |  ram          |         EW2         |
| 2   |  rani         |         EW1         |
| 3   |  ram          |         EW3         |
| 4   |  rani         |         EW4         |
| 6   |  ram          |         EW5         |
|-------------------------------------------|

表b

|-----|---------------|-------|--------------|
| id  | name          |  pid  |  price       |
|-----|---------------|-------|--------------|
| 1   |  soap         |  EW1  |   2000       |
| 2   |  towel        |  EW2  |   1333       |
| 3   |  bed          |  EW3  |   3000       |
| 4   |  facewash     |  EW4  |    250       |
| 5   |  T.soap       |  EW5  |    300       |
|--------------------------------------------|

我需要使用 php mysql 下表给出的结果

|-----------------|-----------|---------------------|
| no of products  | name      |  total_ price       |
|-----------------|-----------|---------------------|
|       3         |  ram      |          4833       |
|       2         |  rani     |          2250       |
|---------------------------------------------------|
4

3 回答 3

1
SELECT COUNT(b.id), a.name, SUM(b.price)
FROM a LEFT JOIN b ON a.pid=b.pid
GROUP BY a.name
于 2013-04-10T09:46:35.357 回答
0
$query = " SELECT count(tableA.name) ,tableA.name,sum(tableB.price)
           FROM tableA
           INNER JOIN tableB 
              ON tableA.pid=tableB.pid
           GROUP BY tableA.name "
于 2013-04-10T09:52:10.003 回答
0

SELECT COUNT(a.id) AS NO_OF_PRODUCTS, a.name AS NAME, SUM(b.price) AS TOTAL_PRICE FROM a LEFT JOIN b ON b.pid=a.pid GROUP BY a.name

于 2013-04-10T09:52:30.597 回答