查询:
(
SELECT
upd.uid,
upd.update_id,
upd.update,
upd.date,
upd.type,
upd.total_likes,
upd.total_comments,
upd.deleted,
usr.username AS `username`,
usr.profile_picture AS `profile_picture`
,(
SELECT
COUNT(lik.id)
FROM
likes as lik
WHERE
upd.update_id = lik.item_id
AND
lik.uid = 118697835834
AND lik.type=0
) as liked_update,
(
SELECT
COUNT(fav.id)
FROM
favorites as fav
WHERE
upd.update_id = fav.item_id
AND
fav.uid = 118697835834
AND fav.type=0
) as favorited_update
FROM
updates AS upd
LEFT JOIN
users AS usr
ON upd.uid = usr.uid
WHERE
upd.deleted=0
AND
(
upd.uid=118697835834
OR EXISTS
(
SELECT *
FROM
subscribers AS sub
WHERE
upd.uid = sub.suid
AND sub.uid = 118697835834
)
)
)
UNION
(
SELECT
topic.uid,
topic.tid,
topic.title,
topic.body,
topic.total_likes,
topic.total_replies,
topic.views,
topic.date,
usr.username AS `username`,
usr.profile_picture AS `profile_picture`
,(
SELECT
COUNT(lik.id)
FROM
likes as lik
WHERE
topic.update_id = lik.item_id
AND
lik.uid = 118697835834
AND lik.type=1
) as liked_update,
(
SELECT
COUNT(fav.id)
FROM
favorites as fav
WHERE
topic.update_id = fav.item_id
AND
fav.uid = 118697835834
AND fav.type=1
) as favorited_update
FROM
topics AS topic
LEFT JOIN
users AS usr
ON topic.uid = usr.uid
WHERE
topic.deleted=0
AND
(
topic.uid=118697835834
OR EXISTS
(
SELECT *
FROM
subscribers AS sub
WHERE
topic.uid = sub.suid
AND sub.uid = 118697835834
)
)
)
ORDER BY date DESC
我添加了UNION ( SELECT )
并且查询需要永远加载,最后它会停止返回blank page
...
删除UNION ( SELECT )
可以正常工作......
答案 0 :(得分:1)
确定所有评论后,请说明您为使脚本正常工作所做的更改。
根据您对来自同一列的两个表的数据的评论,因为您已通过使用UNION告诉引擎执行此操作。如果你想要它们在单独的列中,你需要在它们之间放置Null:
SELECT
upd.uid,
upd.update_id,
upd.update,
upd.date,
upd.type,
upd.total_likes,
upd.total_comments,
upd.deleted,
null AS `topicuid`,
null AS `topictid`,
null AS `topictitle`,
null AS `topicbody`,
null AS `topictotal_likes`,
null AS `topictotal_replies`,
null AS `topicviews`,
null AS `topicdate`,
usr.username AS `username`,
usr.profile_picture AS `profile_picture`,
(SELECT COUNT(lik.id) FROM likes as lik WHERE upd.update_id = lik.item_id AND lik.uid = 118697835834 AND lik.type=0) as liked_update,
(SELECT COUNT(fav.id) FROM favorites as fav WHERE upd.update_id = fav.item_id AND fav.uid = 118697835834 AND fav.type=0) as favorited_update
FROM
......
UNION ALL
SELECT
null AS `upd.uid`,
null AS `upd.update_id`,
null AS `upd.update`,
null AS `upd.date`,
null AS `upd.type`,
null AS `upd.total_likes`,
null AS `upd.total_comments`,
null AS `upd.deleted`,
topic.uid,
topic.tid,
topic.title,
topic.body,
topic.total_likes,
topic.total_replies,
topic.views,
topic.date,
usr.username AS `username`,
usr.profile_picture AS `profile_picture`,
(SELECT COUNT(lik.id) FROM likes as lik WHERE topic.update_id = lik.item_id AND lik.uid = 118697835834 AND lik.type=1) as liked_update,
(SELECT COUNT(fav.id) FROM favorites as fav WHERE topic.update_id = fav.item_id AND fav.uid = 118697835834 AND fav.type=1) as favorited_update
您可能需要对列名做更多的工作,但是您应该到达那里。我没有对这个问题进行投票,但我可能应该这样做。您应该了解UNION运算符对两个或多个查询实际执行的操作。
答案 1 :(得分:0)
当您使用UNION时,您必须具有完全相同的结果“列格式,名称,...”,因此您在查询upd.total_comments的顶部作为total_comments
,并且您将topic.total_replies作为{ {1}}这些是不同的名字。希望您理解我尝试解释的内容,如果不是,请访问here,以便更好地解释。