简体   繁体   English

SQL返回重复的行

[英]SQL returning rows duplicated

I've been developing an extremely basic feed system within my website which (is supposed to) display all the posts where: 我一直在网站中开发一个非常基本的Feed系统,该系统应该(显示)显示以下所有帖子:

  1. The posts are not from the logged in user 帖子不是来自登录用户
  2. They are all from friends 他们都来自朋友
  3. And are ordered by post_id (to get the most recent ones first) post_id排序(首先获取最新的)

One problem I am having with my SQL though, is that when querying the database tables to grab the rows, it will return one post, but it'll be posted by all users, instead of just the one person that created it. 我的SQL遇到的一个问题是,当查询数据库以获取行时,它将返回一个帖子,但是它将被所有用户发布,而不仅仅是创建它的一个人。

For example, if user 1 posted the status "Hi" , and there are 5 users in the database; 例如,如果用户1的状态为"Hi" ,则数据库中有5个用户; it'll grab that post, then print it to the page 5 times, along with each individual user as the poster. 它将抓取该帖子,然后将其与每个用户作为海报一起打印到页面5次。 (See below for image example) (请参见下面的图像示例)

SQL : SQL

SELECT posts.*, users.*, friends.* 
FROM posts, users, friends
WHERE posts.poster <> ? 
AND users.user_id = posts.poster
AND friends.areFriends = 1 AND (friends.userA = ? AND friends.userB = posts.poster) 
OR friends.areFriends = 1 AND (friends.userA = posts.poster AND friends.userB = ?)
ORDER BY posts.post_id desc LIMIT 20

For reference: 以供参考:

posts.poster is the person who created the post posts.poster是创建帖子的人
users.user_id is the id of the same person friends.areFriends 0 = not a friend, 1 = friend friends.userA / friends.userB is the logged in user or the friend that that user is friends with (if that makes sense) users.user_id是同一个人的id friends.areFriends users.user_id 0 =不是朋友,1 =朋友friends.userA / friends.userB是已登录的用户或该用户与之成为朋友的朋友(如果有意义)

错误图片

I have tried every possible combination of things that I know to fix the problem, however I'm still new to "advanced" SQL and can't seem to find anything 我已经尝试过解决已知问题的所有可能组合,但是我对“高级” SQL还是陌生的,似乎找不到任何东西

I've attempted to explain my problem as best I can, so hopefully someone can help me. 我试图尽我所能解释我的问题,所以希望有人可以帮助我。 I've been trying for the last couple of days to fix it but to no avail, 最近几天我一直在尝试修复它,但无济于事,

Cheers. 干杯。

It should be because of your where conditions. 应该是因为您where条件。 Change it like this: 像这样更改它:

WHERE posts.poster <> ? 
AND users.user_id = posts.poster
AND friends.areFriends = 1 
AND ((friends.userA = ? AND friends.userB = posts.poster) OR (friends.userA = posts.poster AND friends.userB = ?))

As you wrote the conditions, it will return the rows that fulfill all other conditions OR friends.areFriends = 1 AND (friends.userA = posts.poster AND friends.userB = ?) . 当您编写条件时,它将返回满足所有其他条件的行,或者friends.areFriends = 1 AND (friends.userA = posts.poster AND friends.userB = ?) So even the rows that don't fulfill users.user_id = posts.poster will be returned. 因此,即使users.user_id = posts.poster的行也将被返回。 But as I wrote the conditions, the OR is enclosed in a parenthesis. 但是当我写条件时,OR括在括号中。

A classic way of making inner joins is more readable and helps you to understand why the code does not work. 进行内部联接的经典方法更具可读性,可帮助您理解为什么代码不起作用。 Also, it is kind of code convention for SQL, so it also helps other people (like me) to understand you. 另外,它是SQL的一种代码约定,因此它也可以帮助其他人(例如我)理解您。

I have re-wrote your code to use INNER JOIN . 我已经重新编写了您的代码以使用INNER JOIN Try this out. 试试看

SELECT @userId := ?;

SELECT posts.*, users.*, friends.* 
FROM friends
JOIN users ON users.user_id = 
    CASE friends.userA
    WHEN @userId
    THEN friends.userB
    ELSE friends.userA END
JOIN posts ON posts.poster = users.user_id
WHERE friends.userA = @userId OR friends.userB = @userId
ORDER BY posts.post_id DESC LIMIT 20;

Make sure that there are no dupes in friends table. 确保friends表中没有重复对象。 Following query will help you with this: 以下查询将帮助您:

SELECT User1, User2, COUNT(*) as DupeCount
FROM 
(
    SELECT
        CASE WHEN friends.userA < friends.userB THEN friends.userA ELSE friends.userB END AS User1,
        CASE WHEN friends.userA < friends.userB THEN friends.userB ELSE friends.userA END AS User2
    FROM friends
) AS F
GROUP BY User1, User2
HAVING COUNT(*) > 1

I assume the query must return posts of the friends of the user ? 我假设查询必须返回用户朋友的帖子? . First find friends, the their posts, then their other data. 首先找到朋友,他们的帖子,然后他们的其他数据。

SELECT  pp.*, uu.* 
FROM     (select distinct case when ? = friends.userB then friends.userA else friends.userB end as user_id
         from friends 
         where ? in (friends.userA, friends.userB)
         and friends.areFriends = 1 
         ) f
join posts pp on pp.poster = f.user_id
join users uu on uu.user_id = pp.poster

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM