我的查询有3个UNION
正在运行,我需要添加第四个。
查询是:
SELECT SQL_CACHE posts.*
FROM (
SELECT posts.id, posts.text, posts.image, posts.date, posts.status, posts.location as postLocation, posts.user AS user, users.name, users.username, users.photo, users.location,users.bio, users.background, groups_posts.group as groupId, null as groupName, sum(r.rating) as rating
,(
select count(re.id )
FROM relations re
WHERE re.user = users.id
) as numfollowing,
(
select count(re.id )
FROM relations re
WHERE re.friend = users.id
) as numfollowers,
(
select count(re.id)
FROM relations re
where re.friend = users.id
AND re.user = 24527
) as following,
(
select count(c.id)
from comments c
where c.post = posts.id AND c.user NOT IN (
select users.id from users where users.status = 3 OR users.status = 4
)
) as numcomments,
ifnull(ru.rating, 0) as myrating
FROM `relations`
JOIN `posts` ON relations.friend = posts.user
JOIN `users` ON relations.friend = users.id
LEFT JOIN `groups_posts` ON posts.id = groups_posts.post
LEFT JOIN ratings r ON r.post = posts.id
LEFT JOIN ratings ru ON ru.post = posts.id and ru.user = 24527
WHERE relations.user = '24527'
AND groups_posts.group is NULL
AND posts.status != 1 AND posts.status != 5
AND posts.date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
GROUP BY posts.id
UNION
SELECT posts.id, posts.text, posts.image, posts.date, posts.status, posts.location as postLocation, posts.user AS user, users.name, users.username, users.photo, users.location, users.bio, users.background, null as groupId, null as groupName, sum(r.rating) as rating
,(
select count(re.id )
FROM relations re
WHERE re.user = users.id
) as numfollowing,
(
select count(re.id )
FROM relations re
WHERE re.friend = users.id
) as numfollowers,
(
select count(re.id)
FROM relations re
where re.friend = users.id
AND re.user = 24527
) as following,
(
select count(c.id)
from comments c
where c.post = posts.id AND c.user NOT IN (
select users.id from users where users.status = 3 OR users.status = 4
)
) as numcomments,
ifnull(ru.rating, 0) as myrating
FROM `posts`
JOIN `users` ON posts.user = users.id
LEFT JOIN `groups_posts` ON posts.id = groups_posts.post
LEFT JOIN ratings r ON r.post = posts.id
LEFT JOIN ratings ru ON ru.post = posts.id and ru.user = 24527
WHERE posts.user = '24527'
AND groups_posts.group IS NULL
AND posts.status != 1 AND posts.status != 5
AND posts.date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
GROUP BY posts.id
UNION
SELECT posts.id, posts.text, posts.image, posts.date, posts.status, posts.location as postLocation, posts.user AS user, users.name, users.username, users.photo, users.location, users.bio, users.background, groups.id as groupId, groups.name as groupName, sum(r.rating) as rating
,(
select count(re.id )
FROM relations re
WHERE re.user = users.id
) as numfollowing,
(
select count(re.id )
FROM relations re
WHERE re.friend = users.id
) as numfollowers,
(
select count(re.id)
FROM relations re
where re.friend = users.id
AND re.user = 24527
) as following,
(
select count(c.id)
from comments c
where c.post = posts.id AND c.user NOT IN (
select users.id from users where users.status = 3 OR users.status = 4
)
) as numcomments,
ifnull(ru.rating, 0) as myrating
FROM groups_members
JOIN groups ON groups_members.group = groups.id
JOIN groups_posts ON groups_members.group = groups_posts.group
JOIN posts ON groups_posts.post = posts.id
JOIN users ON posts.user = users.id
LEFT JOIN ratings ru ON ru.post = posts.id and ru.user = 24527
LEFT JOIN ratings r ON r.post = posts.id
WHERE groups_members.member = '24527'
AND groups_members.subscription = '1'
AND groups.status = '1'
AND posts.status != 1 AND posts.status != 5
AND posts.date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
GROUP BY posts.id
UNION
-- I need to add the SELECT below
SELECT companies_posts.id as `posts.id`,
companies_posts.text as `posts.text`,
companies_posts.image as `posts.image`,
companies_posts.date as `posts.date`,
companies_posts.status as `posts.status`,
null as postLocation,
companies_posts.company AS user,
companies.name as `users.name`,
companies.username as `users.username`,
companies.photo as `users.photo`,
null as `users.location`,
companies.description as `users.bio`,
companies.cover as `users.cover`,
null as groupId,
null as groupName,
null as rating,
null as numfolllowing,
null as numfollowers,
null as following,
null as myrating,
null as numcomments
FROM `companies_subscriptions`
JOIN `companies_posts` ON companies_subscriptions.user = user
JOIN `companies` ON companies_subscriptions.company = companies.id
WHERE companies_subscriptions.user = '24527'
AND companies_posts.status != 1 AND companies_posts.status != 5
AND companies_posts.date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
GROUP BY companies_posts.id
ORDER BY posts.date DESC, groupId DESC
) as posts
LEFT JOIN users_blocked ON (users_blocked.user = posts.user AND users_blocked.blocked_friend = 24527) OR (users_blocked.user = 24527 AND users_blocked.blocked_friend = posts.user)
WHERE
users_blocked.user IS NULL
AND users_blocked.blocked_friend IS NULL
GROUP BY posts.id
ORDER BY posts.date DESC
LIMIT 0, 10
修复了很多错误之后,它正在获取...但是在获取时返回错误1054,如下图所示:
发生了什么事?