问题
My query is:
SELECT temp.pid FROM
(SELECT postid, date FROM swapping AS s, post AS p
WHERE s.mid='2' AND p.postid=s.postid) AS temp
WHERE temp.date = (SELECT MAX(date) FROM temp)
I receive #1146 - Table 'databasename.temp' doesn't exist
How can I make it work? Thank you.
回答1:
It seems like you want to select the last "pid", in terms of "date", where s.mid='2'
Try this (after you figure out where pid comes from and correct the first line)
SELECT [s? or maybe p?].pid
FROM swapping s INNER JOIN post p ON p.postid=s.postid
WHERE s.mid = '2'
ORDER BY date DESC
LIMIT(0,1)
You might also need to alias the date column in the order by line.
回答2:
I think you have your column incorrect...
SELECT temp.pid FROM ( SELECT postid, ...
should be
SELECT temp.postid FROM ( SELECT postid, ...
回答3:
@DRapp hit the nail on the head at least. You haven't selected 'pid' (if that column exists in either the swapping or post table) in your sub selection that your referring to as temp so it would throw some type of error there.
来源:https://stackoverflow.com/questions/5263876/sql-as-table-doesnt-exist-1146