如何获取两个单独的SQL查询的UNION并仅获取DISTINCT值

时间:2010-12-28 21:15:42

标签: sql mysql

如何在PHP中仅从我的这两个查询中获取不同的值:

$query1 = "SELECT g.name, g.hash, g.views
                    FROM c7_groups g, c7_groups_affiliations ga
                    WHERE g.category >= 592 AND g.category <= 622
                        AND ga.affiliated_groups_hash = g.hash
                        AND LOWER(g.name) LIKE LOWER('%$school_name%')
                        ";
        $query2 = "SELECT g.name, g.hash, g.views
                    FROM c7_groups g, c7_groups_affiliations ga, c7_affiliations a
                    WHERE ga.affiliations_id = a.id
                        AND ga.groups_hash = g.hash
                        AND LOWER(a.name) LIKE LOWER('%$school_name%')";

我想从这两个查询中只获得不同的“g.hash”。如果我可以有一个SQL查询来完成这两项任务,那就更好了。

我的数据库表的详细信息如下: c7_groups: id int(10)
name varchar(255)
hash varchar(32) groups_categories_id int(10) views int(10)

c7_affiliations id int(10) name varchar(100)
groups_categories_id int(10)

c7_groups_affiliations groups_hash varchar(32)affiliated_groups_hash varchar(32)-------- FKey to c7_groups.hash
affiliations_id int(10)-------------------- FKey to c7_affiliations.id
id int(10)

affiliated_groups_hash&amp; affiliations_id可以为null,或者我应该说一个或它们总是为空。如果一个群组隶属于另一个群组,则affiliations_id将为null,如果该群组隶属于另一个从属关系,则affiliated_groups_hash将为null。

如果您需要任何有关任何内容的详细信息,请与我们联系。

此致 Zeeshan

3 个答案:

答案 0 :(得分:2)

只需在子选择中进行联合,然后按派生表进行分组。

select name,count(*) from
(
               SELECT g.name, g.hash, g.views
                    FROM c7_groups g, c7_groups_affiliations ga
                    WHERE g.category >= 592 AND g.category <= 622
                        AND ga.affiliated_groups_hash = g.hash
                        AND LOWER(g.name) LIKE LOWER('%$school_name%')
     UNION
               SELECT g.name, g.hash, g.views
                    FROM c7_groups g, c7_groups_affiliations ga, c7_affiliations a
                    WHERE ga.affiliations_id = a.id
                        AND ga.groups_hash = g.hash
                        AND LOWER(a.name) LIKE LOWER('%$school_name%')";
) as myTbl
group by name

答案 1 :(得分:1)

试试这个

SELECT Distinct 
           g.name, g.hash, g.views
                    FROM c7_groups g, c7_groups_affiliations ga
                    WHERE g.category >= 592 AND g.category <= 622
                        AND ga.affiliated_groups_hash = g.hash
                        AND LOWER(g.name) LIKE LOWER('%$school_name%')
UNION
SELECT 
           g.name, g.hash, g.views
                    FROM c7_groups g, c7_groups_affiliations ga, c7_affiliations a
                    WHERE ga.affiliations_id = a.id
                        AND ga.groups_hash = g.hash
                        AND LOWER(a.name) LIKE LOWER('%$school_name%')

答案 2 :(得分:1)

似乎第一个查询和第二个查询之间的唯一区别是有一个连接。也许如果您使用ANSI-92连接,它就会像这样,并且不需要UNION。

          SELECT DISTINCT 
                   g.name, 
                   g.hash,
                   g.views
            FROM 
                    c7_groups g
                  INNER JOIN  c7_groups_affiliations ga
                  ON ga.affiliated_groups_hash = g.hash
                  LEFT JOIN c7_affiliations a
                   on ga.affiliations_id = a.id

            WHERE 
                (LOWER(g.name) LIKE LOWER('%$school_name%')
                 AND g.category >= 592 AND g.category <= 622 
                 AND  a.ID is null)
                OR 
                 (LOWER(a.name) LIKE LOWER('%$school_name%')
                 and a.ID is NOT null)

从技术上讲,第二部分中的a.ID is NOT null不是必需的,因为LOWER(a.name) LIKE LOWER('%$school_name%')会这样做,但它会使发生的事情更加清晰