数据库计划: http://pastebin.com/aPXk1rMf
我想从“评论”表中选择“帖子所有者的 uName ”和“评论所有者的 uName ”(即我认为我需要cOwnerID> uName和pID&gt ; uID> uName)以便我可以在发布评论后立即记录一些活动。 我尝试使用MySQL连接以实现这一点,但我不能;所以这就是我徒劳的尝试。
$result = dbquery("INSERT INTO comments VALUES ('','$pID','$ownerID','$cmnt')"); // inserting a comment
$lastID = mysql_insert_id();
$res = dbquery("SELECT
users.username AS username
FROM `comments`
LEFT JOIN `users` on comments.cOwnerID = users.uID
WHERE (cID='$lastID')");
答案 0 :(得分:1)
SELECT users1.username, users2.username FROM Posts p
WHERE p.pID="$POST_OF_INTEREST"
LEFT JOIN users users1 ON users1.uID=p.uID
LEFT OUTER JOIN comments ON comments.pID=p.pID
LEFT OUTER JOIN users users2 ON users2.uID=comments.cOwnerID
根据特定的帖子ID,这将获得发布者的用户名和评论该帖子的每个人的用户名(每条评论一行)。
答案 1 :(得分:1)
我认为你必须为表用户使用别名,因为实际上,你将在这个表上有两个连接:一个用于帖子作者,另一个用于评论的作者。
尝试这样的事情:
SELECT
commentAuthor.username AS commentauthor_username,
postAuthor.username AS postauthor_username
FROM `comments`
LEFT JOIN `users` AS commentAuthor on comments.cOwnerID = commentAuthor.uID
LEFT JOIN `posts` on posts.pId = comments.pID
LEFT JOIN `users` AS postAuthor on posts.uID = postAuthor.uID
WHERE (cID='$lastID')
答案 2 :(得分:1)
SELECT Users.uName AS UserNamePost
, UserComment.uName AS UserNameComment
FROM Users
INNER JOIN Posts
ON Posts.uID = Users.uID;
INNER JOIN Comments
ON Comments.pID = Posts.pID ;
INNER JOIN Users AS UserComment
ON Comments.cOwnerID = UserComment.uID ;
这样做的目的是让发布帖子的用户然后加入用户以获取帖子的所有评论
答案 3 :(得分:0)
“SELECT users.username AS用户名, comments.cOwnerID AS commentOwner, users.uID AS userID 从 评论 LEFT JOIN 用户 上 commentOwner = userID 哪里 commentOwner ='{$ lastID}'';