我有以下代码:
SELECT * FROM uc_redwables AS T1
INNER JOIN
(SELECT dwable , MAX(time) AS time FROM uc_redwables WHERE EXISTS (SELECT * FROM `uc_following` WHERE `userid` = '5' AND find_in_set(followed, redwabledby) > 0) OR find_in_set('5',`redwabledby`)
GROUP BY dwable
) AS T2
ON T1.dwable = T2.dwable AND T1.time = T2.time
ORDER by T2.time desc LIMIT 20
这可以按预期工作。
但是当我把它放在我的大问题中时:
SELECT
p.`id` as id,
p.`postinguser` as postinguser,
p.`page` as page,
p.`likedby` as likedby,
p.`redwabled` as redwabled,
case r.`redwabledby`
when (SELECT `followed` FROM `uc_following` WHERE `userid` = '5') then r.`time`
when '5' then r.`time`
else p.`time`
end as date_time
FROM `uc_posts` p left join
(
//Code entered here
SELECT * FROM uc_redwables AS T1
INNER JOIN
(SELECT `dwable` , MAX(time) AS time FROM uc_redwables WHERE EXISTS (SELECT * FROM `uc_following` WHERE `userid` = '5' AND find_in_set(followed, redwabledby) > 0) OR find_in_set('5',`redwabledby`)
GROUP BY `dwable`
) AS T2
ON T1.dwable = T2.dwable AND T1.time = T2.time
ORDER by T2.time desc LIMIT 20
//Code entered ends here
) r on p.id = r.dwable
WHERE p.`postinguser` = '5' OR find_in_set('5',p.`redwabled`) OR EXISTS (SELECT * FROM `uc_users` WHERE `id` = '5' AND find_in_set(postinguser, following) > 0) OR EXISTS (SELECT * FROM `uc_following` WHERE `userid` = '5' AND find_in_set(followed, redwabled) > 0)
order by date_time desc LIMIT 20
我收到Duplicate column name 'dwable'
错误。
我认为这是因为它在其他地方使用过。我已经查找了许多不同的解决方案,包括使用别名,但我似乎无法获得任何工作。
有人可以帮忙吗?
由于
答案 0 :(得分:0)
此代码非常难以理解,我不认为您知道如何使用CASE
或FIND_IN_SET
,但我认为我理解这是错误的。
当您运行一段有效的代码时,您将返回一个结果集,该结果集包含两个名为dwable
的列 - 一个来自T1
,另一个来自T2
- 很好,直到你left join
时将它变成派生表,因为表的列名必须是唯一的。基本上如果没有别的,改变:
//Code entered here
SELECT * FROM uc_redwables AS T1
INNER JOIN
(SELECT `dwable` , MAX(time) AS time FROM uc_redwables WHERE EXISTS (SELECT * FROM `uc_following` WHERE `userid` = '5' AND find_in_set(followed, redwabledby) > 0) OR find_in_set('5',`redwabledby`)
GROUP BY `dwable`
) AS T2 ON T1.dwable = T2.dwable AND T1.time = T2.time
ORDER by T2.time desc LIMIT 20
//Code entered ends here
要:
//Code entered here
SELECT * FROM uc_redwables AS T1
INNER JOIN
(SELECT `dwable` as dwable_2 , MAX(time) AS time FROM uc_redwables WHERE EXISTS (SELECT * FROM `uc_following` WHERE `userid` = '5' AND find_in_set(followed, redwabledby) > 0) OR find_in_set('5',`redwabledby`)
GROUP BY dwable_2
) AS T2 ON T1.dwable = T2.dwable_2 AND T1.time = T2.time
ORDER by T2.time desc LIMIT 20
//Code entered ends here
此 应该。