关于MYSQL一条多表查询语句的问题。


表group 字段 group_id level
表people 字段 people_id group_id
表relation 字段 relation_id people_id relate_people_id group_id

现在想查询 relation 表的 relation_level relation.people_id 的人的 people_level 高的关系出来,应该怎么写语句比较合适?我写的SQL语句如下,希望大家能提点一二。


 SELECT
    a.people_id,
    a.relation_level,
    b.people_level
FROM
    (
        SELECT
            people_id,
            level AS relation_level
        FROM
            `relation`
        JOIN `group` ON `relation`.group_id = `group`.group_id
    ) AS a,
    (
        SELECT
            people_id,
            level AS people_level
        FROM
            `people`
        JOIN `group` ON `people`.group_id = `group`.group_id
    ) AS b
WHERE
    a.people_id = b.people_id
AND a.relation_level > b.people_level

感谢您能耐心看完我的问题。

数据库 mysql 数据查询

各种的虎 11 years, 2 months ago

Your Answer