加入两个表,但仍然可以获得第一个表的完整数据

时间:2015-12-24 09:00:35

标签: mysql sql

首先我有表users

+---------+----------+------------------+
| user_id | username |      email       |
+---------+----------+------------------+
|       1 | User 1   | email1@gmail.com |
|       2 | User 2   | email2@gmail.com |
|       3 | User 3   | email3@gmail.com |
|       4 | User 4   | email4@gmail.com |
+---------+----------+------------------+

接下来我有表user_announcement

+---------+----------+---------+-----------+
| user_id | annou_id | is_read | read_time |
+---------+----------+---------+-----------+
|       1 |        1 |       0 | Time      |
|       2 |        1 |       1 | Time      |
|       1 |        2 |       0 | Time      |
|       2 |        3 |       1 | Time      |
+---------+----------+---------+-----------+

我正在尝试计算如何获取所有用户及其公告ID 1的读取状态 我想输出有这样的东西

+---------+----------+------------------+----------+---------+-----------+
| user_id | username |      email       | annou_id | is_read | read_time |
+---------+----------+------------------+----------+---------+-----------+
|       1 | User 1   | email1@gmail.com | 1        | 0       | Time      |
|       2 | User 2   | email2@gmail.com | 1        | 1       | Time      |
|       3 | User 3   | email3@gmail.com | NULL     | NULL    | NULL      |
|       4 | User 4   | email4@gmail.com | NULL     | NULL    | NULL      |
+---------+----------+------------------+----------+---------+-----------+

我尝试了各种各样的联接,但它没有给我我想要的结果。

4 个答案:

答案 0 :(得分:2)

执行LEFT JOIN时,如果需要过滤第二个表中的列,则应该在ON子句中执行此操作。

SELECT *
FROM user u
LEFT JOIN user_announcement ua
ON u.user_id=ua.user_id AND ua.annou_id = 1

答案 1 :(得分:1)

编辑: 根据用户要求更新了答案

这就是你要找的东西。在这里,我首先为每个用户获取min(annou_id),然后获取该行的其他记录。现在将此派生表视为第二个表,我将使用前一个左连接来获取所需的记录。

select u.user_id,u.username,u.email,ua_derived.annou_id,ua_derived.is_read,ua_derived.read_time
from user u
left join 
    (
            select ua1.user_id,ua1.annou_id,ua1.is_read,ua1.read_time from user_announcement ua1
        inner join 
            (select user_id,min(annou_id) as annou_id  from user_announcement
            group by user_id 
            ) ua2
        on ua1.user_id=ua2.user_id
        and ua1.annou_id=ua2.annou_id
    ) ua_derived
on u.user_id=ua_derived.user_id;

SQL小提琴演示

http://sqlfiddle.com/#!9/57a74/4

=============================================== =========================

上一回答:

答案与Yeldar相同,但您只需要使用列别名来显示。

select u.user_id,u.username,u.email,ua.annou_id,ua.is_read,ua.read_time
from user u
left join user_announcement ua
on u.user_id=ua.user_id

SQL小提琴演示

http://sqlfiddle.com/#!9/db28e/1

答案 2 :(得分:0)

根据w3schools

  

using AutoMapper.QueryableExtensions; // This using is required [EnableQuery] [HttpGet] public IHttpActionResult Get() { var query = db.ConfigSets.ProjectTo<ConfigSetDTO>(); return Ok(query); } 关键字返回左表中的所有行,右表中包含匹配的行。当没有匹配时,结果是LEFT JOIN在右侧。

这正是你所需要的:

NULL

答案 3 :(得分:0)

是的,你可以试试这个

select
   t1.*,
   t2.annou_id,
   t2.is_read,
   t2.read_time
from
   user t1
   left join user_announcement t2
      on t1.user_id = t2.user_id
where
   t2.annou_id = 1
order by
   t1.user_id desc