每个派生表都必须有自己的别名

时间:2011-12-15 18:14:45

标签: mysql join random derived

我有以下查询:

SELECT `snap`.`ID`, `user`.`username`, `vote`.`type` 
FROM (`snap`) JOIN `user` as u ON `u`.`ID` = `snap`.`user` 
LEFT JOIN (select * from vote where user = "18") as vote ON `snap`.`ID` = `vote`.`snap` 
JOIN (SELECT CEIL(MAX(ID)*RAND()) AS ID FROM snap)) AS x ON `snap`.`ID` => `x`.`ID` 
WHERE `snap`.`active` = 0 LIMIT 1

它工作得很好,直到我添加了最后一个JOIN。现在我得到错误:“每个派生表必须有自己的别名”。我知道这是因为每个表都需要它的别名,我需要把“as S”或其他东西放在某个地方,但我不知道如何在这个查询中做到这一点。

2 个答案:

答案 0 :(得分:4)

看起来你在snap之后有额外的右括号。它应该是1而不是2个右括号。

SELECT `snap`.`ID`, `user`.`username`, `vote`.`type` 
FROM (`snap`) JOIN `user` as u ON `u`.`ID` = `snap`.`user` 
LEFT JOIN (select * from vote where user = "18") as vote ON `snap`.`ID` = `vote`.`snap` 
JOIN (SELECT CEIL(MAX(ID)*RAND()) AS ID FROM snap) AS x ON `snap`.`ID` = `x`.`ID` 
WHERE `snap`.`active` = 0 LIMIT 1

答案 1 :(得分:1)

正确的语法是:

SELECT `snap`.`ID`, `user`.`username`, `vote`.`type`
FROM (`snap`) JOIN `user` as u ON `u`.`ID` = `snap`.`user`
LEFT JOIN (select * from vote where user = "18") as vote ON `snap`.`ID` = `vote`.`snap`
JOIN (SELECT CEIL(MAX(ID)*RAND()) AS ID FROM snap) AS x ON `snap`.`ID` = `x`.`ID`
WHERE `snap`.`active` = 0 LIMIT 1