duanpin2034 2013-05-17 12:08
浏览 23

具有NULL值的Mysql多连接表

I'm trying to do the following...

I have a PHPmyadmin database wit the tables: tasks, users and a table task_user. For example I want to have all tasks from 2013-01-01, and a task can have multiple users. I want to make a table in HTML which looks like this:

                      Name1       Name2       Name3
Date: 01-01-2013      Task                    Task
                      Task

So the query should always return the users wether they have tasks or not.

If I do something like this:

SELECT t.title, u.firstname, u.lastname FROM users as u
INNER JOIN task_user as tu ON tu.uid = u.uid
INNER JOIN tasks as t ON t.taskid = tu.taskid
WHERE t.date = "2013-05-16"

This only returns ONE user, this user has TWO tasks on this date, but it also only displays ONE task.

How can I get this query to give me all users with all of their tasks? (if they have tasks, else return null) Or does anybody know a better way to achieve this?

I think I might be needing some other JOIN or a GroupBy?

All help will be appreciated!

  • 写回答

1条回答 默认 最新

  • doucitao2944 2013-05-17 12:11
    关注

    Use LEFT or RIGHT joins instead of INNER joins. Inner joins will only return records if a record is found in both / all tables

    SELECT t.title, u.firstname, u.lastname FROM users as u
    LEFt JOIN task_user as tu ON tu.uid = u.uid
    LEFT JOIN tasks as t ON t.taskid = tu.taskid
    WHERE t.date = "2013-05-16"
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么