Actually SQL query:

SELECT a.id, a.postId, a.creatorId, a.targetId, a.groupId, a.text, a.content, a.app, a.access, a.created, a.publishedByTitle, a.publishedByDate, a.publishedPostId FROM activities a, friends f, groupmembers gm, groups g WHERE (a.creatorId IN (SELECT F.friendTwoId FROM friends F, users U WHERE F.friendOneId = 1 AND F.friendTwoId = U.uid) ) OR ( a.creatorId IN (SELECT F.friendOneId FROM friends F, users U WHERE F.friendOneId = U.uid AND F.friendTwoId = 1 AND F.status = 1) ) OR (a.groupId IN (SELECT g.gid FROM groups g, groupmembers gm WHERE g.gid in(SELECT groupId FROM groupmembers WHERE memberId = 1) AND g.gid = gm.groupId GROUP BY g.gid) ) AND a.id < 10 AND a.creatorId <> 1 GROUP BY a.id ORDER BY a.created DESC LIMIT 10 

The rule AND a.id < 10 does not work, no reaction. Displays everything, even where id is greater than 10

  • 2
    Because the priority of the OR operation is lower than that of AND, and your AND a.id < 10 now only applies to the last block after the last OR. The same applies to the condition AND a.creatorId <> 1 . - nzeemin
  • 2
    you have a request like someCondition OR a.id < 10 , i.e. when someCondition is triggered, a.id is not checked, so it can be anything. Most likely, your conditions of the form (a.creatorId IN (...)) OR (a.creatorId IN (...)) OR... wrap in brackets - BOPOH

1 answer 1

Place the group of conditions joined by the OR operator in parentheses

 SELECT a.id, a.postId, a.creatorId, a.targetId, a.groupId, a.text, a.content, a.app, a.access, a.created, a.publishedByTitle, a.publishedByDate, a.publishedPostId FROM activities a, friends f, groupmembers gm, groups g WHERE ( (a.creatorId IN (SELECT F.friendTwoId FROM friends F, users U WHERE F.friendOneId = 1 AND F.friendTwoId = U.uid) ) OR (a.creatorId IN (SELECT F.friendOneId FROM friends F, users U WHERE F.friendOneId = U.uid AND F.friendTwoId = 1 AND F.status = 1) ) OR (a.groupId IN (SELECT g.gid FROM groups g, groupmembers gm WHERE g.gid in(SELECT groupId FROM groupmembers WHERE memberId = 1) AND g.gid = gm.groupId GROUP BY g.gid) ) ) AND a.id < 10 AND a.creatorId <> 1 GROUP BY a.id ORDER BY a.created DESC LIMIT 10 
  • thanks helped - Shuhratjon Jumaev