简体   繁体   English

SQL查询从一个表获取所有记录,除特定记录,按日期,从另一个表

[英]SQL Query To Get All Records From One Table, Except A Specific Record, By Date, From Another Table

I am trying to SELECT records from two MySql tables. 我试图从两个MySql表中选择记录。 I would like all records from the first table excluding specific records, by date, from the second table. 我希望第一个表中的所有记录从第二个表中按日期排除特定记录。 For example: 例如:

Table1 T1id , firstName , LastName 表1 T1idfirstNameLastName
Table2 id , T1id , hours , rate , date 表2 idT1idhoursratedate

T1id is the link between the two tables, therefore when the tables are joined I would have T1id , firstName , lastName , hours , rate , date T1id是两个表之间的链接,因此当表连接时,我会有T1idfirstNamelastNamehoursratedate

Let's say there is a record in Table2 with a date of 2012-02-08. 假设表2中有一条记录,其date为2012-02-08。 With one query, I need to select all records from Table1 , excluding the record from Table2 that has the date of 2012-02-08 . 使用一个查询,我需要从Table1中选择所有记录, 不包括Table2中具有日期2012-02-08的记录

I've tried a few variations of JOINS and UNIONS, however I either get all records, a bunch of duplicate records, or one record (ie Table2 date). 我尝试了JOINS和UNIONS的一些变体,但是我要么获得所有记录,一堆重复记录,要么记录一次(即Table2日期)。 I apologize, but I do not have a specific piece of code to include since nothing has worked for me. 我道歉,但我没有特定的代码片段,因为没有什么对我有用。

USE INNER JOIN if you are sure that T1id exists in both tables: 如果您确定两个表中都存在T1id,请使用INNER JOIN

SELECT  a.T1id, 
        a.FirstName, 
        a.LastName, 
        b.hours, 
        b.rate, 
        b.date
FROM table1 a INNER JOIN table2 b
        ON a.T1id = b.T1id
WHERE b.date <> DATE('2012-02-08')

but if you want to get all T1id from Table1 (which exists or does not exists in Table2 ) use LEFT JOIN 但是如果你想从Table1中获取所有T1id (表2 中存在或不存在 ),请使用LEFT JOIN

SELECT  a.T1id, 
        a.FirstName, 
        a.LastName, 
        b.hours, 
        b.rate, 
        b.date
FROM table1 a LEFT JOIN table2 b
        ON a.T1id = b.T1id
WHERE b.date <> DATE('2012-02-08')

So many times this is typically done with a NOT EXISTS subquery, but subqueries can be big performance hits in larger tables... However, by doing a LEFT JOIN and looking for NULL is in essence, the same result 这么多次通常使用NOT EXISTS子查询来完成,但是子查询可以在较大的表中进行大的性能命中...但是,通过执行LEFT JOIN并且查找NULL本质上是相同的结果

select
      t1.*
   from
      table1 t1
         left join table2 t2
            on t1.t2.t1id
          AND t2.date = '2012-02-08'
   where
      t2.t1id IS NULL

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL查询可从一个表中获取所有记录,并在另一个表上进行连接,包括任何其他唯一记录 - SQL Query to get all records from one table and join on another including any additional unique records SQL查询-从一个表中选择全部,在另一个表中匹配记录 - SQL Query - select all from one table with matching records in another SQL 查询:删除表中除最新N条记录外的所有记录? - SQL query: Delete all records from the table except latest N? SQL 查询在一个表中搜索记录并将其替换为另一个表中的多条记录 - SQL query to search for a record in one table and replace it with multiple records from another table SQL查询以获取除ID来自另一个表的数组之外的所有内容 - SQL query to get all except where id is in array that comes from another table 我想连接数据并显示一个表中的所有记录和另一个表中的一个记录 - I want to join data and display all records from one table and just one record from another table SQL 查询返回一个表中的记录,这些记录分别与另一张表中的 2 条记录相关联 - SQL query to return records from one table that are associated to 2 records each from another table 编写SQL查询以根据另一个表中的值获取所有记录(非联接情况) - Write SQL query to get all records based on value from another table (not join case) 在一个查询中从两个表中选择一个记录,并从另一个表中选择多个记录 - Selecting one record from two tables and multiple records from another table in ONE query 查询以检索一个表中的主记录以及来自另一表的所有子记录 - Query to retrieve main records in one table and its all sub records from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM