Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.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
Mysql 从子查询中访问主表数据_Mysql_Sql - Fatal编程技术网

Mysql 从子查询中访问主表数据

Mysql 从子查询中访问主表数据,mysql,sql,Mysql,Sql,假设我有一个“用户”和“课程”表,这两个表由一个“用户课程”表关联。这允许1个用户拥有多个课程等 “我的用户”表包含100万用户,我希望: 选择特定年龄和性别的用户 和 选择他们与其他给定用户共有的确切课程数 我的方法是选择性别和年龄与我所寻找的相匹配的用户,这相当容易 接下来,我执行一个左连接,并使用一个子查询获得所有用户的计数以及与给定用户共有的课程数 问题是,在子查询中,我必须重新选择所有用户,并且在子查询中不重复性别=‘女性’和出生年份介于‘1991’和‘1993’之间,它将选择所有10

假设我有一个“用户”和“课程”表,这两个表由一个“用户课程”表关联。这允许1个用户拥有多个课程等

“我的用户”表包含100万用户,我希望:

选择特定年龄和性别的用户 和 选择他们与其他给定用户共有的确切课程数 我的方法是选择性别和年龄与我所寻找的相匹配的用户,这相当容易

接下来,我执行一个左连接,并使用一个子查询获得所有用户的计数以及与给定用户共有的课程数

问题是,在子查询中,我必须重新选择所有用户,并且在子查询中不重复性别=‘女性’和出生年份介于‘1991’和‘1993’之间,它将选择所有100万用户

SELECT u . *, matching_courses_count FROM users u LEFT JOIN (SELECT COUNT(course_id) AS matching_courses_count, uc.user_id FROM users u LEFT JOIN user_courses uc ON u.id = uc.user_id WHERE uc.course_id IN (SELECT course_id FROM user_courses WHERE user_id = 1) AND uc.user_id != 1 GROUP BY uc.user_id) matching_courses ON u.id = matching_courses.user_id WHERE gender = 'female' AND birth_year BETWEEN '1991' AND '1993' SQL Fiddle:


有没有一种方法可以在不必在子查询中再次选择用户或不必在子查询中重复where子句的情况下实现这一点?

下面是一种使用select子句中的相关子查询制定查询的方法。这可以确保您获得满足where条件的所有用户,即使没有匹配的课程

select u.*,
       (select count(*)
        from user_courses uc join
             user_courses uc1
             on uc.course_id = uc1.course_id and
                uc.user_id = u.id and
                uc1.user_id = 1
      ) as matching_courses_count
from users u
where u.gender = 'female' and
      birth_year BETWEEN 1991 AND 1993;

相关子查询只计算用户和用户1之间的课程数。

我认为您想要的可以这样完成。这个想法是你选择你感兴趣的用户。然后你得到他们所学的课程。然后从课程表中获取具有相同课程id的其他行,最后获取参加这些课程的用户

在where子句中,将结果限制为具有所需条件的用户。并筛选出用户1和用户2相同的结果

    select *
    from users u1
    left join user_courses uc1 
    on u1.id = uc1.user_id
    left join user_courses uc2
    on uc2.course_id = uc1.course_id
    left join users u2
    on uc2.user_id = u2.id

    where u1.id = 1 and
          u2.gender = 'female' and u2.birth_year between '1991' and '1993' and
          u1.id != u2.id
然后,您可以将此结果包装到另一个查询中,以进行公共课程计数,如下所示:

select id, count(*) from (
        select uc1.course_id, u2.id
        from users u1
        left join user_courses uc1 
        on u1.id = uc1.user_id
        left join user_courses uc2
        on uc2.course_id = uc1.course_id
        left join users u2
        on uc2.user_id = u2.id

        where u1.id = 1 and
              u2.gender = 'female' and u2.birth_year between '1991' and '1993' and
              u1.id != u2.id) as x
    group by id

嗨,戈登,谢谢你的回答。这将是一种干净的方法,希望尝试在子查询中访问u.id时失败,因为“on子句”中有未知列“u.id”。有什么想法吗?@Scottcalvin。这应该与外部查询中的from users u子句相关联。如果你提出一个SQL提琴,我可以让它工作。我已经更新了问题,包括模式和我的查询的SQL提琴。谢谢你回答汉斯,但是有几件事遗漏了;匹配课程的计数、我们传入的用户ID和最终分组的用户。很高兴听到你将如何做到这一点。