当条件基于另一个SELECT的结果时选择SELECT

时间:2017-11-18 14:51:54

标签: mysql select inner-join

查询#1按预期工作,但每次“smf_user_points_log.id_school_year”不会是“7”。该值(而不是7)必须是查询#2的结果。

查询#1:

SELECT smf_user_points_log.id_member, smf_members.usertitle
        FROM smf_user_points_log
        INNER JOIN smf_members
        ON smf_members.id_member=smf_user_points_log.id_member
        WHERE smf_user_points_log.id_school_year = 7 AND smf_user_points_log.points >= 50 AND smf_members.date_registered < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY))

查询#2:

SELECT id_school_year FROM smf_settings

如何重写Query#1以获得相同的结果?我尝试了各种连接,但没有成功。

3 个答案:

答案 0 :(得分:0)

你可以做子查询:

mapStateToProps

答案 1 :(得分:0)

您可以将R与子查询一起使用:

IN

此外,请考虑使用简短且有意义的别名来提高查询可读性。

答案 2 :(得分:0)

您可以加入smf_settings表格。

SELECT smf_user_points_log.id_member, smf_members.usertitle
FROM smf_user_points_log
INNER JOIN smf_members
    ON smf_members.id_member=smf_user_points_log.id_member
INNER JOIN smf_settings
    ON smf_user_points_logs.id_school_year = smf_settings.id_school_year
WHERE smf_user_points_log.points >= 50 
    AND smf_members.date_registered < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY))