Subquery selecting over a select
I have a table users
structured as follow:
userUID (int), userName (varchar), userImage (varchar)
And a table 'posts' structured as follow
postPID (int), postUID (int), postMessage (char)
Now, the postUID
correspond to the author UID
.
When i SELECT
the posts, i'd like to also select the userName
and userImage
field corresponding to the 开发者_JAVA百科postUID
.
How could i do that?
SELECT p.*, u.userName, u.userImage
FROM users u
JOIN posts p
ON p.postUID = u.userUID
Select
p.postPID,
p.postUID,
p.postMessage,
u.userName,
u.userImage
From posts p
Join users u
On p.postUID = u.userUID
It's better to name your columns instead of selecting * as has been discussed on here in numerous threads.
It depends on how many records are returned. If not sure it is good to have top 10
or top 100
so that it only returns that many rows.
精彩评论