Skip to content
Advertisement

how to filter data in sql

I am trying to filter the data according to several criteria, and based on these criteria, the record record number 7 (M08) should not appear because the “NewSiteID” column is not null and does not belong to the number 6, but the problem is that this record appears even though I used the conditions that appear in the query.

       tbMachines.MachIsTransfered, tbMachines.NewSiteID
FROM tbMachines
WHERE BranchID = 6 AND MachIsTransfered = 0
      OR      BranchID = 6 AND MachIsBorrowed = 1
      OR      BranchID = 6 AND MachIsloaning = 1
      AND NewSiteID = 6 OR NewSiteID = '' ```

[enter image description here][1]


  [1]: https://i.stack.imgur.com/yhkUP.png

Advertisement

Answer

You need to use parenthesis around your conditions. In this case it can also be simplified quite a bit. If I understand what you are trying to do it could be something like this.

WHERE BranchID = 6 
    AND 
    (
        MachIsTransfered = 0
        OR
        MachIsBorrowed = 1
        OR 
        MachIsloaning = 1
    )
    AND NewSiteID in (6, '')
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement