Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MySQL查询,用于查找具有约束的三个表之间的关系,即使该关系不存在';不存在_Php_Mysql_Laravel - Fatal编程技术网

Php MySQL查询,用于查找具有约束的三个表之间的关系,即使该关系不存在';不存在

Php MySQL查询,用于查找具有约束的三个表之间的关系,即使该关系不存在';不存在,php,mysql,laravel,Php,Mysql,Laravel,我在一所拥有用户、课程和用户到课程状态的在线大学工作。我有一个用户列表和另一个课程列表。我想找到所有给定用户和课程的课程状态,包括用户尚未开始的课程的空状态 例如: User IDs: [1, 7, 14, 21] Course IDs: [5, 8, 36, 50] 预期结果: Name Course Status John Doe How to Tie your Shoes Complete John Doe

我在一所拥有用户、课程和用户到课程状态的在线大学工作。我有一个用户列表和另一个课程列表。我想找到所有给定用户和课程的课程状态,包括用户尚未开始的课程的空状态

例如:

User IDs: [1, 7, 14, 21]  
Course IDs: [5, 8, 36, 50]
预期结果:

Name        Course                     Status
 John Doe    How to Tie your Shoes      Complete
 John Doe    How to Paint your House    In Progress
 Jane Doe    How to Tie your Shoes      Complete
 Jane Doe    How to Paint your House    Not Started <-- These are the tricky ones
关于如何写这样的东西有什么想法吗?另外,请告诉我是否可以提供更多详细信息或更多代码/表格示例

编辑:以下是我使用以下答案中的建议更新的查询:

SELECT
    `users`.`name` AS `Name`,
    `users`.`email` AS `Email`,
    `courses`.`number` AS `Course #`,
    `courses`.`name` AS `Course`,
    COALESCE(`courses_users_statuses`.`name`, 'Not Started') AS `Status`
FROM
    `users`
        LEFT JOIN
    `courses_users` ON `courses_users`.`user_id` = `users`.`id`
        LEFT JOIN
    `courses` ON `courses`.`id` = `courses_users`.`course_id` AND `courses`.`id` IN (1, 2, 3, 4, 5)
        LEFT JOIN
    `courses_users_statuses` ON `courses_users_statuses`.`id` = `courses_users`.`status_id`
WHERE
    `users`.`id` IN (1, 2, 3, 4, 5)
ORDER BY
    `partners`.`name`,
    `users`.`name`,
    `courses`.`number`
这个更新的示例是一个改进,但现在它显示的记录中没有课程名称或编号,但有一个状态。我不确定它是如何为本应存在的课程关系赢得地位的。相反,它们应该为NULL(或“未启动”)。以下是数据库中的一些示例数据:

`users`
表格:

id    name             email
1     Stevie McComb    test@example.com
2     John Doe         test@example.org
3     Jane Doe         test@example.net
`courses`
表格:

id    number    name
1     101       Navigation
2     102       Logging In
3     103       Updating Records
4     104       Managing Users
course_id    user_id    status_id    completed_at
1            1          2            2017-01-01 00:00:00
3            1          1            2017-01-05 00:23:00
1            2          2            2017-04-13 15:00:37
id    name           slug
1     In Progress    progress
2     Complete       complete
`courses\u users`
表格:

id    number    name
1     101       Navigation
2     102       Logging In
3     103       Updating Records
4     104       Managing Users
course_id    user_id    status_id    completed_at
1            1          2            2017-01-01 00:00:00
3            1          1            2017-01-05 00:23:00
1            2          2            2017-04-13 15:00:37
id    name           slug
1     In Progress    progress
2     Complete       complete
`courses\u users\u status`
表格:

id    number    name
1     101       Navigation
2     102       Logging In
3     103       Updating Records
4     104       Managing Users
course_id    user_id    status_id    completed_at
1            1          2            2017-01-01 00:00:00
3            1          1            2017-01-05 00:23:00
1            2          2            2017-04-13 15:00:37
id    name           slug
1     In Progress    progress
2     Complete       complete
预期结果:

Name             Email               Course #    Course              Status
Stevie McComb    test@example.com    101         Navigation          Complete
Stevie McComb    test@example.com    102         Logging In          Not Started
Stevie McComb    test@example.com    103         Updating Records    In Progress
Stevie McComb    test@example.com    104         Managing Users      Not Started
John Doe         test@example.org    101         Navigation          Complete
John Doe         test@example.org    102         Logging In          Not Started
John Doe         test@example.org    103         Updating Records    Not Started
John Doe         test@example.org    104         Managing Users      Not Started
Jane Doe         test@example.net    101         Navigation          Not Started
Jane Doe         test@example.net    102         Logging In          Not Started
Jane Doe         test@example.net    103         Updating Records    Not Started
Jane Doe         test@example.net    104         Managing Users      Not Started
当前结果:

Name             Email               Course #    Course              Status
Stevie McComb    test@example.com                                    Complete
Stevie McComb    test@example.com                                    Not Started
Stevie McComb    test@example.com    103         Updating Records    In Progress
Stevie McComb    test@example.com                                    Not Started
John Doe         test@example.org    101         Navigation          Complete
John Doe         test@example.org                                    Not Started
John Doe         test@example.org                                    Not Started
John Doe         test@example.org                                    Not Started
Jane Doe         test@example.net                                    Not Started
Jane Doe         test@example.net                                    Not Started
Jane Doe         test@example.net                                    Not Started
Jane Doe         test@example.net                                    Not Started

问题是您将约束放在where子句中,而不允许
null
值。。这实际上将
左连接
更改回
内部连接

要解决这个问题,您可以显式地允许null,也可以将逻辑移到join子句(我的首选项)


问题是您将约束放在where子句中,而不允许
null
值。。这实际上将
左连接
更改回
内部连接

要解决这个问题,您可以显式地允许null,也可以将逻辑移到join子句(我的首选项)


当我必须处理可能为空的值时,我使用

IFNULL(field_name, 'use this value instead');
比如说

SELECT
IFNULL(Course, 'Nothing Found') AS course
FROM
Course
Where....
或者您可以在WHERE子句中指定

WHERE Course IS NOT NULL

当我必须处理可能为空的值时,我使用

IFNULL(field_name, 'use this value instead');
比如说

SELECT
IFNULL(Course, 'Nothing Found') AS course
FROM
Course
Where....
或者您可以在WHERE子句中指定

WHERE Course IS NOT NULL

我完全忘记了可以向联接添加约束。这也许能解决问题。今天,我会尝试这个,一旦我有一个改变,再次工作的项目,并告诉你,如果我遇到任何其他问题。@史蒂文斯科姆:如果这回答了你的问题,请考虑投票/接受适当的答案。我之所以提到这一点,是因为你是新来的,很抱歉耽搁了,直到今天早上我才有机会再做这个项目。这让我更接近,因为它在以前没有的区域返回空值…但现在它让我感到新的头痛。返回的记录中随机缺少课程名称和/或编号。甚至在课程状态为“完成”或“进行中”的情况下也会发生这种情况,这意味着那里没有空值,但由于某些原因,课程详细信息丢失。例如:Name=>'John Doe',Course#=>,Course Name=>,Status=>'Complete',听起来这些用户好像没有参加指定的课程。您定义查询的方式将返回输入ID的所有用户,无论他们是否参加了课程。这是预期的,但每个用户都有多个课程。因此,即使用户没有参加,它也应该列出给定的课程名称/编号,状态显示为“未开始”或空值。但它显示的是一个状态,而不是课程名称。我不确定用户没有尝试过的课程的“开始”或“进行中”状态来自何处。这些应该是空的,但它们并不总是空的。我检查了表,数据都很好,但由于某种原因,查询变得混乱。我已经用我最近的查询更新了这个问题。我完全忘记了你可以给连接添加约束。这也许能解决问题。今天,我会尝试这个,一旦我有一个改变,再次工作的项目,并告诉你,如果我遇到任何其他问题。@史蒂文斯科姆:如果这回答了你的问题,请考虑投票/接受适当的答案。我之所以提到这一点,是因为你是新来的,很抱歉耽搁了,直到今天早上我才有机会再做这个项目。这让我更接近,因为它在以前没有的区域返回空值…但现在它让我感到新的头痛。返回的记录中随机缺少课程名称和/或编号。甚至在课程状态为“完成”或“进行中”的情况下也会发生这种情况,这意味着那里没有空值,但由于某些原因,课程详细信息丢失。例如:Name=>'John Doe',Course#=>,Course Name=>,Status=>'Complete',听起来这些用户好像没有参加指定的课程。您定义查询的方式将返回输入ID的所有用户,无论他们是否参加了课程。这是预期的,但每个用户都有多个课程。因此,即使用户没有参加,它也应该列出给定的课程名称/编号,状态显示为“未开始”或空值。但它显示的是一个状态,而不是课程名称。我不确定用户没有尝试过的课程的“开始”或“进行中”状态来自何处。这些应该是空的,但它们并不总是空的。我检查了表,数据都很好,但由于某种原因,查询变得混乱。我已经用我最近的查询更新了这个问题。