Program-Me-Rev
Program-Me-Rev

Reputation: 6624

How to make multiple `OR` and `AND` in SQL

I have been trying to retrieve data from a MySQL DB. The following query doesn't yield anything:

SELECT * 
FROM ENTITY_RELATIONSHIPS_TABLE 
WHERE ((SUBJECT_GUID = ? AND TARGET_GUID = ?) 
       OR (TARGET_GUID = ? AND SUBJECT_GUID = ?)) 
  AND RELATIONSHIP_TYPE_VALUE_ID IN (?)  

Removing ((SUBJECT_GUID = ? AND TARGET_GUID = ?) OR (TARGET_GUID = ? AND SUBJECT_GUID = ?)) makes the query work meaning that it it the faulty part.

This works:

SELECT *  
FROM ENTITY_RELATIONSHIPS_TABLE 
WHERE RELATIONSHIP_TYPE_VALUE_ID IN (?)  

This is how I append the prepared data values:

let todo = [
    relGUIDs[0], relGUIDs[1],
    relGUIDs[1], relGUIDs[0],
]  

for (let i = 0; i < relIdsArr.length; i++) todo.push(relIdsArr[i]);  

How can I get the query (below) to work?

SELECT * 
FROM ENTITY_RELATIONSHIPS_TABLE 
WHERE ((SUBJECT_GUID = ? AND TARGET_GUID = ?) 
        OR (TARGET_GUID = ? AND SUBJECT_GUID = ?)) 
  AND RELATIONSHIP_TYPE_VALUE_ID IN (?)  

Thank you all in advance.

Upvotes: 0

Views: 66

Answers (1)

Titus
Titus

Reputation: 22474

(SUBJECT_GUID = ? AND TARGET_GUID = ?) and (TARGET_GUID = ? AND SUBJECT_GUID = ?) is the same thing because you always compare SUBJECT_GUID to relGUIDs[0] and TARGET_GUID to relGUIDs[1], also, todo should have only 5 values and the fifth value should be an array.

I'm not sure what you're trying to do but I think you're looking for something like this:

let todo = [
    relGUIDs[0], relGUIDs[1],
    relGUIDs[0], relGUIDs[1],
    relIdsArr
]

Upvotes: 1

Related Questions