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

Tinpont
  • 162

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

现在想查询 relation表的 relation_levelrelation.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

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

回复
阅读 3.2k
你知道吗?

宣传栏