there are two different tables. I want to find the expressions with “%USD” in the columns of the same name in these two tables do not match and list them on the screen.
Table 1 query;
<?php include("db.php"); $queb = $conn->query("select distinct symbol from tableA WHERE symbol LIKE '%USD'"); while ($resb = $queb->fetch_assoc()){ $symbolb = $resb['symbol']; } ?>
table 2 query;
<?php include("db.php"); $quec = $conn->query("select distinct symbol from tableB WHERE symbol LIKE '%USD'"); while ($resc = $quec->fetch_assoc()){ $symbolc = $resc['symbol']; } ?>
How can I list expressions that are not between two tables?
Advertisement
Answer
I may not be understanding your question fully. If you want to list symbols that begin with ‘USD’ that are in tableA
but not in tableB
along with symbols in tableB
that begin with USD
that are not in tableA
, then the following query should return those symbols:
select distinct symbol from tableA where symbol LIKE '%USD' and symbol not in (select distinct symbol from tableB) union select distinct symbol from tableB where symbol LIKE '%USD' and symbol not in (select distinct symbol from tableA)
Or you can use outer joins instead:
select distinct symbol from tableA left join tableB on tableA.symbol = tableB.symbol where tableA.symbol like ('%USD') and tableB.symbol is null union select distinct symbol from tableB left join tableA on tableB.symbol = tableA.symbol where tableB.symbol like ('%USD') and tableA.symbol is null
In either case, performance will be improved in you have indexes on the symbol
columns.