如何在MySQL中组合这两个查询?

时间:2013-01-04 18:22:12

标签: mysql sql

好的,我是个菜鸟,我接受了。但我正在尝试将这两个查询结合起来,由于某些原因我无法理解。

查询1.

select t.id from tickets t

查询2.

SELECT name 
FROM groups 
WHERE id = (
            SELECT DISTINCT immediateparentid 
            FROM cachedgroupmembers c
            LEFT JOIN groups g
            ON c.groupid = g.id
            LEFT JOIN tickets t
            ON t.id = g.instance
            WHERE t.Id = 1234
            AND g.type = 'AdminCc'
            AND immediateparentid <> c.groupid
        )

第一个查询中的故障单ID正在第二个查询的where子句中使用。有没有办法将这两个查询结合起来?

1 个答案:

答案 0 :(得分:0)

SELECT 
        g.name,
        (SELECT id FROM tickets) as ID
FROM groups as g
WHERE id = (
            SELECT 
                DISTINCT immediateparentid 
            FROM cachedgroupmembers c
            LEFT JOIN groups g ON c.groupid = g.id
            LEFT JOIN tickets t ON t.id = g.instance
            WHERE t.Id = ID
            AND g.type = 'AdminCc'
            AND immediateparentid <> c.groupid
        )       

这是一个从属子查询