如何从下面的三个表查询

时间:2018-07-14 06:29:00

标签: mysql

我有三个表项目,daily_activity和completed_works。 每个项目的日常活动可能有所不同。每天的活动有几部作品。您可以查看下表以获得完整的想法。

项目表:

------------------------------------
|   PROJECT_ID  |   PROJECT_NAME    |
------------------------------------
|       1       |       PROJ1       |
------------------------------------    
|       2       |       PROJ2       |
------------------------------------    

日常活动表:

 ---------------------------------------------------
|   DA_ID   |   PROJECT_ID      |       DA_DATE     |
 ---------------------------------------------------
|   1       |       1           |       20/12/2018  |
 ---------------------------------------------------
|   2       |       1           |       21/12/2018  |
 ---------------------------------------------------
|   3       |       2           |       23/12/2018  |
 ---------------------------------------------------

completed_works表:

----------------------------------------------------------------------------
|   ID  |       DA_ID       |       work_code       |       work_desc       |
----------------------------------------------------------------------------
|   1   |       1           |           w001        |       work desc. 1    |
----------------------------------------------------------------------------
|   2   |       2           |           w002        |       work desc. 2    |
----------------------------------------------------------------------------
|   3   |       2           |           w003        |       work desc. 3    |
----------------------------------------------------------------------------
|   4   |       1           |           w004        |       work desc. 4    |
----------------------------------------------------------------------------    
|   5   |       3           |           w005        |       work desc. 5    |
----------------------------------------------------------------------------    

我想根据项目ID获取完成的作品记录。首先根据项目ID搜索da_id,然后从基于da_id的已完成作品中获取记录,主要是我要获取da_date,例如,在下表中,您可以看到work_code = w001在DA_ID = 1下,而DA_DATE已完成= 20/12/2018。

所需结果

----------------------------------------------------------------------------
|   ID  |   work_code       |       work_desc       |       DA_DATE         |
----------------------------------------------------------------------------
|   1   |       w001        |       work desc. 1    |       20/12/2018      |
----------------------------------------------------------------------------
|   2   |       w002        |       work desc. 2    |       21/12/2018      |
----------------------------------------------------------------------------
|   3   |       w003        |       work desc. 3    |       21/12/2018      |
----------------------------------------------------------------------------
|   4   |       w004        |       work desc. 4    |       20/12/2018      |
----------------------------------------------------------------------------

我已经尝试过,但是我不明白如何获取da_date。这是我的查询

SELECT * FROM completed_works
WHERE DA_ID IN (SELECT D.ID FROM daily_activity as d WHERE PROJECT_ID = 1)

在此查询中,我可以使用IN子句获取PROJECT_ID为1下的DA_ID。但是不知道如何在DA_DATE获得它。

1 个答案:

答案 0 :(得分:2)

这通常是通过联接表来完成的。我一直喜欢这样:

SELECT *
FROM completed_works, daily_activity, project,
WHERE completed_works.DA_ID = daily_activity.DA_ID AND
      aily_activity.PROJECT_ID = project.PROJECT_ID;

这很容易理解。但是大多数人实际上会使用JOIN语法:

SELECT *
FROM completed_works
JOIN daily_activity ON completed_works.DA_ID = daily_activity.DA_ID
JOIN project ON daily_activity.PROJECT_ID = project.PROJECT_ID;

两个查询都是等效的。参见:https://dev.mysql.com/doc/refman/8.0/en/join.html

您可以选择所需的列,而不使用*。当添加联接类型时,联接会变得很复杂。上面的联接实际上是内部联接。

enter image description here