我正在尝试做一个相对简单的MySQL查询,我返回用户添加的最喜欢的帖子。但是,我唯一的错误是他最喜欢的帖子的名称返回为添加收藏的用户,而不是撰写帖子的实际用户。下面是我的收藏夹数据库,然后是下面的查询。如果您需要我的更多信息,请告诉我,因为我很乐意以任何方式提供帮助。
$username = $_GET['username'];
//initial query
$query = "SELECT body, latitude, longitude, (SELECT username FROM Users WHERE Users.IDUser=Favorites.IDUser) AS username, (SELECT profile_picture FROM Users WHERE Users.IDUser=Favorites.IDUser) AS profile_picture, filename, post_date FROM Posts, Favorites WHERE Posts.IDPosts = Favorites.IDPosts AND Favorites.IDUser=(Select IDUser FROM Users WHERE Users.username=:username) ORDER BY post_date DESC";
$query_params = array(
':username' => $username
);
数据库图表:
![在此输入图片说明] [2]
答案 0 :(得分:1)
您应该为此问题上传了数据库架构,并且您应该将查询格式化为更具可读性,但无论如何我都不知道这是否正确,我无法尝试但是试着告诉我,如果它是错的(我不知道我在做什么,因为我看不到架构,我也不知道你想做什么)
SELECT body, latitude, longitude, Users.username,
Users.profile_picture, filename, post_date
FROM Posts, Favorites, Users
WHERE Users.username=:username AND Posts.IDPosts = Favorites.IDPosts
AND Favorites.IDUser = Users.IDUser ORDER BY post_date DESC
修改强>
SELECT Users.profile_picture, Users.username, latitude,
longitude, post_date, filename
FROM Users
JOIN Posts ON Posts.IDUser = Users.IDUser
JOIN Favorites ON Posts.IDPosts = Favorites.IDPosts
WHERE Users.username=:username
AND Users.IDUser = Posts.IDUser
AND Posts.IDPosts = Posts.IDPosts