如果选择值大于0,则MySql INNER JOIN

时间:2017-04-30 09:48:18

标签: mysql

我遇到以下问题,我想提出一个可以返回我的表计划信息的查询。它看起来像这样:

+---------+---------+------------+----------+---------------------+------------------+--------------+----------+------------+---------------+-----------+-------------+---------+
| logs_id | plan_id | workout_id | plan_day | date_made           | workout_names_id | name         | language | workout_id | plan_names_id | name      | language_id | plan_id |
+---------+---------+------------+----------+---------------------+------------------+--------------+----------+------------+---------------+-----------+-------------+---------+
|       2 |       1 |          1 |        1 | 2017-04-30 11:40:48 |                2 | Workout GR 1 |        2 |          1 |             2 | Plan 1 GR |           2 |       1 |
+---------+---------+------------+----------+---------------------+------------------+--------------+----------+------------+---------------+-----------+-------------+---------+
1 row in set (0.00 sec)

我的问题是,当plan_id没有任何值时,我什么也得不到,即使plan_id为0,我怎么能得到一个会返回一些数据的查询。 这是我到目前为止的代码:

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

SELECT * FROM logs AS lo INNER JOIN workout_names AS wn ON lo.workout_id = wn.workout_id INNER JOIN plan_names AS pn ON lo.plan_id = pn.plan_id WHERE wn.language = _language AND pn.language_id = _language ORDER BY lo.logs_id;

END$$

当plan_id中没有值时,deosnt会返回任何内容。

  

已更新!

以下代码适用于部分问题:

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

SELECT * FROM logs AS lo INNER JOIN workout_names AS wn ON lo.workout_id = wn.workout_id LEFT JOIN plan_names AS pn ON lo.plan_id = pn.plan_id WHERE wn.language = _language AND pn.language_id = _language ORDER BY lo.logs_id;


END$$

如果pn.language_id中的值不为null,我该如何才能执行pn.language_id = _language。我有trnnd pn.language_id = _language IS NOT NULL但它返回给我:

+---------+---------+------------+----------+---------------------+------------------+--------------+----------+------------+---------------+-----------+-------------+---------+
| logs_id | plan_id | workout_id | plan_day | date_made           | workout_names_id | name         | language | workout_id | plan_names_id | name      | language_id | plan_id |
+---------+---------+------------+----------+---------------------+------------------+--------------+----------+------------+---------------+-----------+-------------+---------+
|       3 |       1 |          1 |        1 | 2017-04-30 12:20:47 |                1 | Workout EN 1 |        1 |          1 |             1 | Plan 1 EN |           1 |       1 |
|       3 |       1 |          1 |        1 | 2017-04-30 12:20:47 |                1 | Workout EN 1 |        1 |          1 |             2 | Plan 1 GR |           2 |       1 |
+---------+---------+------------+----------+---------------------+------------------+--------------+----------+------------+---------------+-----------+-------------+---------+
2 rows in set (0.00 sec)

基本上它让我解析了所有语言而我只解析了一个,它并没有给我返回NULL。但是如果我添加IS NULL它会返回:

+---------+---------+------------+----------+---------------------+------------------+--------------+----------+------------+---------------+------+-------------+---------+
| logs_id | plan_id | workout_id | plan_day | date_made           | workout_names_id | name         | language | workout_id | plan_names_id | name | language_id | plan_id |
+---------+---------+------------+----------+---------------------+------------------+--------------+----------+------------+---------------+------+-------------+---------+
|       1 |       0 |          1 |        0 | 2017-04-30 10:52:24 |                1 | Workout EN 1 |        1 |          1 |          NULL | NULL |        NULL |    NULL |
+---------+---------+------------+----------+---------------------+------------------+--------------+----------+------------+---------------+------+-------------+---------+
1 row in set (0.00 sec)

我如何将它们合并为一个工作?

1 个答案:

答案 0 :(得分:0)

这是它!

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

    SELECT * FROM logs AS lo INNER JOIN workout_names AS wn ON lo.workout_id = wn.workout_id LEFT JOIN plan_names AS pn ON lo.plan_id = pn.plan_id AND pn.language_id = _language WHERE wn.language = _language ORDER BY lo.logs_id;


    END$$