I'm trying to search for the records of a column called "Students" in table1: if the records are not found in table1 then I should search for them in table2.
Note: Table1 and table2 have this column called "Students" in common.
The expected result is that it shows me the results that were found in table1 in addition to the results that I search for and found in table2, but I don't know if this query is well structured, is there a statement that should be implemented?
I have the following SQL code:
select Alumnos
from tabla1
where Alumnos in (
select Alumnos
from tabla1 a
where not exists(select Alumnos
from tabla2 b
where a.Alumnos = b.Alumnos)
group by Alumnos)
As I commented in Julio 's answer , in MySQL there is the statement . Notice what the documentation says (emphasis mine):
UNION DISTINCT
Therefore, even
UNION
enough, but let 's say just in case you addDISTINCT
.So, the query with
UNION DISTINCT
would be:The truth is that I use Teradata, so I hope this works for you too.
What I would do is
UNION
search first for the students in table 1 and then in table 2. I understand that they can be repeated, so I would add aDISTINCT
. The code is the following:Hope this gives the result you were asking about. Cheers!