mysql - 合并这两个查询

标签 mysql sql merge

我将这两个查询分开列出,但现在我需要将结果放在同一个列表中。

现在我通过使用 javascript 按 id 排序项目来“修复”它。但是从服务器合并这些数据会很棒,这样我就可以分页结果了。

数据库架构

table 上的 friend :

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。我怎样才能在第一时间做同样的事情? (两个查询以相同的格式返回数据)

最佳答案

要合并这两个查询和订单记录的结果,您需要使用 UNION ALL使用 GROUP 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;

关于mysql - 合并这两个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12339182/

相关文章:

asp.net - 使用 Entity Framework 在 asp.net mvc3 中的多个 xml 文档上运行 xpath 查询

sql - 如何在 SQL 中将 json 属性从一个记录值移至新记录值

sql - Rollback 和 RaiseError,哪个先?

sql - 如何获取可能在两行之一中找到的值的计数

mysql - 从 MySQL 获取分层数据

mysql - yum 更新出现 'Obsoleted by ...' 错误

php - Laravel 5.2 查询在本地主机上正常工作,而不是在远程服务器上

javascript - 使用NodeJS在mySQL中插入多条记录以防止注入(inject)并获取每条记录的ID

csv - SSIS - 在将事实与查找表匹配两次时重用 Ole DB 源

python - 使用 groupby 拆分数据框并将子集合并到列中