我将这两个查询单独列出,但现在我需要将结果放在同一个列表中。
现在我通过使用javascript按id排序项目来“修复”它。但是从服务器合并这些数据会很棒,这样我就可以对结果进行分页了..
数据库架构
桌友:
表帮助
查询 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。我怎样才能在第一次做同样的事情?(两个查询都以相同的格式返回数据)