Home > Database > Mysql Tutorial > body text

How Can I Find Missing Records in One Database Table Based on Another Using SQL?

Susan Sarandon
Release: 2024-11-25 10:58:11
Original
343 people have browsed it

How Can I Find Missing Records in One Database Table Based on Another Using SQL?

Identifying Missing Records with "SELECT * WHERE NOT EXISTS"

When working with multiple databases, it may become necessary to identify records in one table that do not exist in another. This can be achieved through the use of the "SELECT * WHERE NOT EXISTS" query.

In a scenario where an "employees" table holds employee details and an "eotm_dyn" table contains additional employee information, it becomes pertinent to determine which employees lack corresponding entries in "eotm_dyn." To accomplish this, a query can be crafted as follows:

SELECT  *
FROM    employees e
WHERE   NOT EXISTS
        (
        SELECT  null
        FROM    eotm_dyn d
        WHERE   d.employeeID = e.id
        )
Copy after login

This query performs a left join between the "employees" and "eotm_dyn" tables on the "employeeID" field, using the "NOT EXISTS" clause to filter out any records in "employees" that do not have a matching entry in "eotm_dyn." The result is a list of all employees who are not represented in the "eotm_dyn" table.

The above is the detailed content of How Can I Find Missing Records in One Database Table Based on Another Using SQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template