I need to select the records those who not exists in the other table.
Companies
company_id | name | temp
-----------------------------
1 | Sony | 1
2 | Samsung | 0
3 | Apple | 1
Technologies
technology_id | company_id
--------------------------
1 | 2
2 | 1
3 | 1
4 | 3
5 | 6
6 | 7
In these two tables you see that the companies in the technologies table with id 6 and 7 do not exist in the companies table. I need to select those and insert it into the companies table.
I tried many things, but get stuck in much more things :P
Last thing I tried is this:
$query_str = "
SELECT company_id
FROM technologies
";
$query = mysqli_query($con, $query_str);
while ($result = mysqli_fetch_assoc($query)) {
$comp = $result['company_id'];
$query_str2 = "
SELECT *
FROM companies
WHERE company_id <> '".$comp."'
";
$query2 = mysqli_query($con, $query_str2);
}
But with this code I get this output: 1 2 ... 1403 1 2 ... 1403 ...
1403 is the amount of records in my company table
To get records those who not exists in the other table just use a simple left join no need for multiple queries
select t.*
from technologies t
left join companies c on t.company_id = c.company_id
where c.company_id is null
You could try doing the work in the database like this perhaps
select * from technologies where company_id not in ( select company_id from companies )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With