Below is an example of the data I have where ID is a foreign key in another table (this table is called test).
+------+------+
| id | data |
+------+------+
| 1 | 100 |
| 1 | 110 |
| 1 | 105 |
| 2 | 105 |
| 2 | 150 |
| 2 | 120 |
| 2 | 320 |
| 3 | 300 |
| 3 | 400 |
| 3 | 500 |
+------+------+
The SELECT statement I have run (which gives me the result I am looking for) is:
select distinct(id) from test where data < 160 and data > 130;
This query retus the foreign key if it matches the criteria in the WHERE clause. This gives me a unique list of foreign keys I can use later.
From my understanding using distinct in this situation doesn't reduce the work done, it only filters the full result set of any duplicates. This means although we have a unique foreign key matched at row:
| 2 | 150 |
The remaining rows are still checked.
Is it possible to make a query which will first check if a match has already been found for a particular foreign key, and it it hasn't only then test the data column with the WHERE clause.
