Find records from one table which don't exist in another MySQL 您所在的位置:网站首页 mysql的join算法 Find records from one table which don't exist in another MySQL

Find records from one table which don't exist in another MySQL

2023-03-31 16:42| 来源: 网络整理| 查看: 265

Answer Option 1

To find records from one table that don’t exist in another table in MySQL, you can use a LEFT JOIN with a WHERE clause that selects records where the value of the primary key in the right table is NULL. Here’s an example query:

SELECT t1.* FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.id WHERE t2.id IS NULL;

In this example, table1 is the table you want to select records from, and table2 is the table you want to compare it to. id is the primary key column in both tables that you want to use for the comparison.

The LEFT JOIN will join all rows from table1 with matching rows from table2. If a row in table1 doesn’t have a matching row in table2, the columns for that row in table2 will be NULL.

The WHERE clause filters the result set to only include rows where t2.id is NULL, which means there was no matching row in table2.

Note that you can modify this query to select columns from table2 instead of table1 if you’re interested in the non-matching records in table2.

Answer Option 2

To find records from one table that do not exist in another table in MySQL, you can use a LEFT JOIN with a WHERE clause to filter the results.

Here’s an example query:

SELECT table1.column1, table1.column2 FROM table1 LEFT JOIN table2 ON table1.column1 = table2.column1 WHERE table2.column1 IS NULL;

In this query, we are selecting the columns we want from table1 and using a LEFT JOIN to join table2 on the matching column (column1 in this example). We then filter the results using a WHERE clause to only show the rows where the column1 value is NULL in table2, indicating that there was no match.

Note that you can modify this query to match on any column or set of columns that you want.



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有