我正在尝试在我的网站上创建一个“供稿”系统,在该系统上发布用户帖子中的活动。为了有效地做到这一点并按日期对所有活动进行排序,我需要将所有内容放在一个巨大的查询中(除非有另一种有效地做到这一点的方法)。我的问题是我当前的“提要”页面有多个ajax调用,以获取所有不同的数据,例如对用户帖子的评论或对用户开始的辩论的投票。有没有一种方法可以组合不相关的查询以返回多个结果?例如,我可以将这两个查询合并为一个吗? 查询1:
SELECT articles.article_id, articles.title, articles.date, articles.idUsers, users.uidUsers, profileimages.image
FROM articles JOIN
users JOIN
profileimages
ON articles.idUsers = users.idUsers AND profileimages.idUsers = users.idUsers
WHERE articles.idUsers = '$following' AND
articles.published = 'yes'
ORDER BY articles.date ASC";
查询2:
SELECT polls.poll_id, polls.title, polls.idUsers, polls.date, users.uidUsers, profileimages.image
FROM polls JOIN
users JOIN
profileimages
ON polls.idUsers = users.idUsers AND profileimages.idUsers = users.idUsers
WHERE polls.idUsers = '$following'
ORDER BY polls.date ASC";
答案 0 :(得分:0)
是的,你可以做到。
但是像我使用它的内部联接会产生很多不需要的结果。
所以您应该使用phpmyadmin或工作台。...
运行一个这样的查询,看看它是否给您正确的结果。 如果不是,则必须将其分组或在条件中添加更多内容。
SELECT
t1.article_id
,t1.title
,t1.idUsers
, t2.poll_id
, t2.title
, t2.idUsers
, t2.date
, t1.uidUsers
, t2.image
FROM
(SELECT
articles.article_id
, articles.title
, articles.date
, articles.idUsers
, users.uidUsers
, profileimages.image
FROM
articles
JOIN users
JOIN profileimages
ON articles.idUsers = users.idUsers AND profileimages.idUsers = users.idUsers
WHERE articles.idUsers = '$following' AND articles.published = 'yes'
ORDER BY articles.date ASC) t1
inner join
(SELECT
polls.poll_id
, polls.title
, polls.idUsers
, polls.date
, users.uidUsers
, profileimages.image
FROM
polls
JOIN users
JOIN profileimages
ON polls.idUsers = users.idUsers AND profileimages.idUsers = users.idUsers
WHERE polls.idUsers = '$following'
ORDER BY polls.date ASC) t2
on t1.uidUsers = t2.uidUsers
ORDER BY t2.date;
答案 1 :(得分:0)
我将全部合并,并在每个查询结果上添加一个新列“类型”。
SELECT
articles.article_id as id,
articles.title as title,
articles.date as date,
articles.idUsers as idUsers,
users.uidUsers as uidUsers,
profileimages.image as image,
'article' as type
FROM articles
JOIN users ON articles.idUsers = users.idUsers
JOIN profileimages on profileimages.idUsers = users.idUsers
WHERE articles.idUsers = '$following' AND articles.published = 'yes'
union all
SELECT
polls.poll_id as id,
polls.title as title,
polls.date as date,
polls.idUsers as idUsers,
users.uidUsers as uidUsers,
profileimages.image as image,
'pool' as type
FROM polls
JOIN users ON polls.idUsers = users.idUsers
JOIN profileimages ON profileimages.idUsers = users.idUsers
WHERE polls.idUsers = '$following'
然后,您可以创建一个超级选择来根据需要对结果进行排序。
select * from (
SELECT
articles.article_id as id,
articles.title as title,
articles.date as date,
articles.idUsers as idUsers,
users.uidUsers as uidUsers,
profileimages.image as image,
'article' as type
FROM articles
JOIN users ON articles.idUsers = users.idUsers
JOIN profileimages on profileimages.idUsers = users.idUsers
WHERE articles.idUsers = '$following' AND articles.published = 'yes'
union all
SELECT
polls.poll_id as id,
polls.title as title,
polls.date as date,
polls.idUsers as idUsers,
users.uidUsers as uidUsers,
profileimages.image as image,
'pool' as type
FROM polls
JOIN users ON polls.idUsers = users.idUsers
JOIN profileimages ON profileimages.idUsers = users.idUsers
WHERE polls.idUsers = '$following'
) query order by query.type, query.date
性能通常与全表扫描有关。确保正确创建了所有索引,并记住在where子句中使用它。 如果结果过多,请考虑实施查询分页。
答案 2 :(得分:0)
您可以在union all
之前join
,因为联接是如此相似:
SELECT ap.article_id, ap.title, ap.date, ap.idUsers,
u.uidUsers, pi.image
FROM ((SELECT a.article_id, a.title, a.date, a.idUsers
FROM articles a
WHERE a.idUsers = '$following' AND a.published = 'yes'
) UNION ALL
(SELECT p. poll_id, p.title, p.date, p.idUsers
FROM posts p
WHERE p.idUsers = '$following'
)
) ap JOIN
users u
ON ap.idUsers = u.idUsers JOIN
profileimages pi
ON pi.idUsers = u.idUsers
ORDER BY ap.date ASC;
注意$following
应该作为参数而不是字符串传递。