I have a table called ‘connections’ with these fields:
entry_id
user_id
connector_id
cat_id
Lets put some rows
| entry_id | user_id | connector_id |cat_id |
|:---------|---------|:------------:|:------------:|
| 1 | 11| 33 | 1
| 2 | 13| 11 | 2
| 3 | 9| 11 | 4
| 4 | 11| 33 | 6
| 5 | 33| 11 | 11
| 6 | 9| 11 | 8
Pseudocode
(using connection between id = 11 and oid = 9)
Select cat_id FROM connections c
if cat_id is between 1 and 5
where c.connector_id = id OR c.user_id = id
AND c.connector_id = oid OR c.user_id = oid
else if cat_id is greater than 5
where oid = user_id and id = connector_id
In english;
If the cat_id is between 1 and 5, it should select cat_ids from where oid and id are both in connector_id or user_id, it doesn’t matter which order as long as they are in one or the other, but if the cat_id is greater than 5, it should only select cat_ids where oid is the user_id and id is the connector_id.
Result (for connection between id = 11 and oid = 9)
| cat_id |
|:-----------|
| 4 |
| 8 |
One more example:
Result (for connection between id = 33 and oid = 11 )
| cat_id |
|:-----------|
| 1 |
| 6 |
Please ask if they’re parts that are not clear.
You should be able to accomplish this by just following your if statements logically. Something like:
Note: in agreement with some of the comments – ensure this is what you need to be doing. I made some assumptions about how you intended your logic to be from the pseudo-code, these may be wrong – so please think about it before just copying and pasting. This specifically applies to how you structured your OR / AND in the where clause.