I recently asked a question here concerning an SQL query: Trouble wrapping head around complex SQL delete query
I now understand that what I'm trying to do is too complex to pull off with a single query or even multiple queries without some way to keep results in between. Therefore I decided to create a bash script (the end result will be something to do with a cronjob so bash is the most straightforward choice).
Consider the following table:
AssociatedClient:
+-----------+-----------------+
| Client_id | Registration_id |
+-----------+-----------------+
| 2 | 2 |
| 3 | 2 |
| 3 | 4 |
| 4 | 5 |
| 3 | 6 |
| 5 | 6 |
| 3 | 8 |
| 8 | 9 |
| 7 | 10 |
+-----------------------------+
What I want to do is select all Registration_id
s where the Client_id
is in the list of Client_id
s associated with a specific Registration_id
.
Although I'm pretty noob with SQL, I found this query relatively easy:
SELECT `Registration_id` FROM `AssociatedClient` ac1
WHERE ac1.`Client_id` IN
(SELECT `Client_id` FROM `AssociatedClient` ac2
WHERE ac2.`Registration_id` = $reg_id);
where $reg_id
is just a bash variable.
This works but I would like to see it done with a self join, because it looks nicer, especially within a bash script where a lot of character clutter occurs. I'm afraid my SQL skills just don't reach that far.