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

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

4

2 回答 2

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
于 2011-12-15T18:18:41.263 回答
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
于 2011-12-15T18:28:06.240 回答