合并这两个查询

时间:2012-09-09 12:47:22

标签: mysql sql merge

我将这两个queriess列为单独的,但现在我需要将结果放在同一个列表中。

现在我通过使用javascript按ID排序项目来“固定”它。但是从服务器合并这些数据会很棒,所以我可以分页结果..

数据库模式

表朋友:

enter image description here

表格帮助

enter image description here

查询A(返回当前用户的结果

   $sql = 'SELECT * FROM helps WHERE id_user ='.$value;

查询B(返回当前用户的朋友的结果

$sql = 'SELECT
    h.*,
    f.* 
    FROM (
            SELECT
                     id  AS friendsId,
                     CASE followerid WHEN '.$value.' THEN followingid ELSE followerid END AS friend_id
                        FROM friends
                        WHERE acepted = 1
                        AND (followerid  = '.$value.' OR followingid = '.$value.')
                    ) AS f
                        INNER JOIN helps AS h ON h.id_user = f.friend_id
                        ORDER BY h.id DESC';

有没有办法合并这些查询?老实说,我不知道怎么做。

- 编辑 -

考虑结合,但我不知道如何处理订单......

$sql = '(SELECT * FROM helps WHERE id_user = '.$value.')
                    UNION
                    (SELECT
                                h.*,
                                f.* 
                            FROM (
                                SELECT
                                    id  AS friendsId,
                                    CASE followerid WHEN '.$value.' THEN followingid ELSE followerid END AS friend_id
                                FROM friends
                                WHERE acepted = 1
                                AND (followerid  = '.$value.' OR followingid = '.$value.')
                            ) AS f
                                INNER JOIN helps AS h ON h.id_user = f.friend_id
                                ORDER BY h.id DESC)';

正如您在第二个查询中看到的,帮助表已重命名为h。我怎么能在第一次做同样的事情? (两个查询都以相同的格式返回数据)

4 个答案:

答案 0 :(得分:4)

要合并这两个查询和订单记录的结果,您需要将UNION ALLGROUP BY一起使用。试试这个问题:

SELECT h.id AS id, h.title, h.content, id_user, h.id_group,
       h.id_type, h.id_lic, h.avatar, h.attached, h.attached,
       h.fetcha, h.likes, h.lan, h.needsCount, h.receivedCount,
       MAX(h.fid) AS fid, MAX(h.friend_id) AS friend_id
FROM (   
      SELECT h.id AS id, h.title, h.content, id_user, h.id_group,
             h.id_type, h.id_lic, h.avatar, h.attached, h.attached,
             h.fetcha, h.likes, h.lan, h.needsCount, h.receivedCount,
             0 AS fid, 0 AS friend_id
      FROM helps h
      WHERE h.id_user ='.$value.'

      UNION ALL

      SELECT h.id, h.title, h.content, id_user, h.id_group,
             h.id_type, h.id_lic, h.avatar, h.attached, h.attached,
             h.fetcha, h.likes, h.lan, h.needsCount, h.receivedCount,
             f.id AS fid, f.friend_id
      FROM (
             SELECT id  AS friendsId,
                    CASE followerid WHEN '.$value.' THEN followingid
                                    ELSE followerid
                    END AS friend_id
             FROM friends
             WHERE acepted = 1
                   AND (followerid  = '.$value.' OR followingid = '.$value.')
            ) AS f
            INNER JOIN helps AS h
                ON h.id_user = f.friend_id
     ) h
GROUP BY h.id
ORDER BY h.id DESC;

答案 1 :(得分:1)

UNION的基本规则是

  • 每个查询返回的列数必须相等且
  • 数据类型必须相互匹配。

如您所见,我没有修改您制定的查询。只是我为每个查询添加了虚拟列。此列将用于订购所有记录。查询由另一个select statment换行,因此虚拟列不会显示在结果列表中,但它将成为排序记录的基础。下面的查询结果包含最顶层用户的记录(因为ORDER BY OrderBy ASC ),后面跟着按朋友ID排序的用户朋友的记录。如果您需要任何澄清,请告知我。还有一件事,我在第二个查询中删除了f.*因为没有必要,并且由于列( number )不匹配而导致语法错误。

SELECT  ID, Title, Content, ID_User, ID_Group, 
        ID_Type, ID_Loc, Avatar, Attached, Fecha, Likes, 
        Lan, NeedsCount, RecivedCount
FROM
    (
        SELECT *, 1 as OrderBy           -- first query you formulated
        FROM helps 
        WHERE id_user = '.$value.'
        UNION
        SELECT  h.*, 2 as OrderBy        -- second query you formulated
        FROM    (
                    SELECT  id  AS friendsId,
                            CASE followerid 
                                WHEN '.$value.' THEN followingid 
                                ELSE followerid 
                            END AS friend_id
                    FROM    friends
                    WHERE   acepted = 1 AND 
                            (followerid  = '.$value.' OR followingid = '.$value.')
                ) AS f
                    INNER JOIN helps AS h 
                        ON h.id_user = f.friend_id
    ) x
ORDER BY OrderBy ASC, ID DESC

答案 2 :(得分:0)

如果两个查询的结果集相似,请尝试使用UNION运算符

答案 3 :(得分:0)

像Omesh描述的那样进行UNION,然后处理排序,执行此操作:

SELECT * FROM
(
    ....Omesh's code....
) T
ORDER BY T.id DESC