DaimCho DaimCho - 2 months ago 9
MySQL Question

Laravel syntax to get querie with distinct results

I have two MySQL tables :

+-----------+ +-----------+
| id.Table1 | | id.Table2 |
+-----------+ +-----------+
| 1 | | 2 |
| 2 | | 3 |
| 3 | | 4 |
| 4 | +-----------+
| 5 |
+-----------+


I want to get a querie with this result:

+-----------+
| id.Table1 |
+-----------+
| 1 |
| 5 |
+-----------+


How do I do it?

Answer

One way is to use NOT IN clause:

SELECT ID FROM TABLE1 WHERE ID NOT IN (SELECT ID FROM TABLE2)