MySql内连接与COUNT

时间:2017-04-29 22:07:55

标签: mysql

我有点卡住了...... 我有3张桌子:

    +---------+----------------------+------+-----+---------+----------------+
    | Field   | Type                 | Null | Key | Default | Extra          |
    +---------+----------------------+------+-----+---------+----------------+
    | plan_id | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
    | image   | varchar(255)         | NO   |     | NULL    |                |
    | level   | enum('E','M','H')    | YES  |     | NULL    |                |
    +---------+----------------------+------+-----+---------+----------------+
    3 rows in set (0.00 sec)

mysql> describe plan_names;
+---------------+------------------------+------+-----+---------+----------------+
| Field         | Type                   | Null | Key | Default | Extra          |
+---------------+------------------------+------+-----+---------+----------------+
| plan_names_id | smallint(5) unsigned   | NO   | PRI | NULL    | auto_increment |
| name          | varchar(255)           | NO   |     | NULL    |                |
| language_id   | smallint(255) unsigned | NO   |     | NULL    |                |
| plan_id       | smallint(255) unsigned | NO   |     | NULL    |                |
+---------------+------------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> describe plan_workouts;
+------------------+------------------------+------+-----+---------+----------------+
| Field            | Type                   | Null | Key | Default | Extra          |
+------------------+------------------------+------+-----+---------+----------------+
| plan_workouts_id | smallint(5) unsigned   | NO   | PRI | NULL    | auto_increment |
| workout_id       | smallint(255) unsigned | NO   |     | NULL    |                |
| plan_id          | smallint(255) unsigned | NO   |     | NULL    |                |
+------------------+------------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

我制作了一个如下所示的存储过程:

DELIMITER $$
CREATE PROCEDURE get_plans(IN _language SMALLINT(255))
BEGIN

SELECT p.plan_id,p.image,p.level,pn.name,pw.plan_workouts_id FROM plan AS p INNER JOIN plan_names AS pn ON p.plan_id = pn.plan_id JOIN plan_workouts AS pw ON p.plan_id = pw.plan_id WHERE pn.language_id =  _language;

#SELECT p.plan_id,p.image,p.level,pn.name FROM plan AS p INNER JOIN plan_names AS pn ON p.plan_id = pn.plan_id ,COUNT(SELECT * FROM plan_workouts WHERE ) WHERE pn.language_id =  _language;

#SELECT p.plan_id,p.image,p.level,pn.name FROM plan AS p INNER JOIN plan_names AS pn ON p.plan_id = pn.plan_id WHERE pn.language_id =  _language AND ;

END $$

正如你所看到的,我解析了一个语言ID,它将返回给我计划。 输出如下:

+---------+-------+-------+-----------+------------------+
| plan_id | image | level | name      | plan_workouts_id |
+---------+-------+-------+-----------+------------------+
|       1 | image | E     | Plan 1 EN |                1 |
|       1 | image | E     | Plan 1 EN |                2 |
|       1 | image | E     | Plan 1 EN |                3 |
|       1 | image | E     | Plan 1 EN |                4 |
|       1 | image | E     | Plan 1 EN |                5 |
|       1 | image | E     | Plan 1 EN |                6 |
|       1 | image | E     | Plan 1 EN |                7 |
|       1 | image | E     | Plan 1 EN |                8 |
|       1 | image | E     | Plan 1 EN |                9 |
|       1 | image | E     | Plan 1 EN |               10 |
|       1 | image | E     | Plan 1 EN |               11 |
|       1 | image | E     | Plan 1 EN |               12 |
|       1 | image | E     | Plan 1 EN |               13 |
|       1 | image | E     | Plan 1 EN |               14 |
|       1 | image | E     | Plan 1 EN |               15 |
|       1 | image | E     | Plan 1 EN |               16 |
|       1 | image | E     | Plan 1 EN |               17 |
|       1 | image | E     | Plan 1 EN |               18 |
|       1 | image | E     | Plan 1 EN |               19 |
|       1 | image | E     | Plan 1 EN |               20 |
+---------+-------+-------+-----------+------------------+
20 rows in set (0.00 sec)

目前在数据库中只有一个计划,但由于计划锻炼表中有20个锻炼,因此它返回20行。我的目标是仅返回计划锻炼的COUNT()。

看起来应该是这样的:

+---------+-------+-------+-----------+------------------+
    | plan_id | image | level | name      | plan_workouts_count |
    +---------+-------+-------+-----------+------------------+
    |       1 | image | E     | Plan 1 EN |                20 |
    +---------+-------+-------+-----------+------------------+

1 个答案:

答案 0 :(得分:2)

您可以将GROUP BYCOUNT一起使用,例如:

SELECT p.plan_id,p.image,p.level,pn.name,COUNT(pw.plan_workouts_id) AS plan_workouts_count 
FROM plan AS p INNER JOIN plan_names AS pn ON p.plan_id = pn.plan_id JOIN plan_workouts AS pw ON p.plan_id = pw.plan_id 
WHERE pn.language_id =  _language;
GROUP BY p.plan_id,p.image,p.level,pn.name;