简体   繁体   English

SQLite查询选择另一个表中不存在的所有记录

[英]SQLite query select all records that does not exist in another table

I am having some problem when trying to perform a SQLite query to get the records which does not exist from another table. 尝试执行SQLite查询以获取另一个表中不存在的记录时遇到问题。 Basically I have two database tables: 基本上我有两个数据库表:

数据库表

My exercise table stored all the exercises available whereas the bookedExercise table store the exercises booked by each users. 我的练习表存储了所有可用的练习,而bookedExercise表存储了每个用户预订的练习。 What I am trying to do is, for example if the exercise does exist in the bookedExercise, it should be filtered out. 我想做的是,例如,如果该练习确实存在于bookedExercise中,则应将其过滤掉。

Here is the SQLite query which I used: 这是我使用的SQLite查询:

SELECT exercise.exerciseID, exercise.exerciseType, exercise.amout FROM exercise LEFT JOIN bookedExercise WHERE exercise.exerciseID = bookedExercise.exerciseID AND bookedExercise.exerciseID IS NULL

However, it returned me empty records. 但是,它返回了我空的记录。 Any ideas? 有任何想法吗?

Thanks in advance. 提前致谢。

如果您不使用联接很好,则可以使用

SELECT * FROM exercise WHERE exerciseID not in (SELECT exerciseID FROM bookedExercise)

Your SQL looked okay... I think the problem might be you have a datatype mismatch. 您的SQL看起来还不错...我认为问题可能是您的数据类型不匹配。 You have exercise ID as an integer in one table and text in another. 您在一个表中将运动ID作为整数,在另一个表中将其作为文本。

Also, if you have huge data volumes, you may want to consider an anti-join: 另外,如果您的数据量很大,则可能需要考虑使用反联接:

select
  e.*
from
  exercise e
where not exists (
  select 1
  from bookedExercise be
  where
    e.excerciseId  = be.exerciseID
)

-- edit -- -编辑-

On second glance, your SQL was not okay. 乍一看,您的SQL不好。 You had your join condition in the where clause. 您在where子句中有加入条件。 This little change would fix your existing SQL: 这个小的更改将修复您现有的SQL:

SELECT exercise.excerciseId , exercise.exerciseType , exercise.amout
FROM exercise LEFT JOIN bookedExercise on
    exercise.excerciseId  = bookedExercise.exerciseID
WHERE bookedExercise.exerciseID IS NULL

When you are using LEFT JOIN, you must put the join condition into the ON clause: 使用LEFT JOIN时,必须将连接条件放入ON子句中:

SELECT exercise.exerciseID,
       exercise.exerciseType,
       exercise.amout
FROM exercise         /* !! */
LEFT JOIN bookedExercise ON exercise.exerciseID = bookedExercise.exerciseID
WHERE bookedExercise.exerciseID IS NULL

暂无
暂无

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

相关问题 查询确实选择并计数另一个表中的记录 - Query that does select and counts records in another table 如何 select 一个表中的所有记录在另一个表中不存在于另一个表中的某些条件下? - How to select all records from one table that do not exist in another table for certain condition in another table? SQL查询-从一个表中选择全部,在另一个表中匹配记录 - SQL Query - select all from one table with matching records in another SQL查询-ID不存在于另一个表中或存在但是所有记录都在历史记录中? - SQL query- ID not exist in another table or exist but with all records are in history? SQL:选择表中存在复合键的选择查询中存在的所有记录 - SQL: Select all records in table that exist in a select query where there is a composite key 如何从一个表中选择另一张表中不存在的所有记录? - How to select all records from one table that do not exist in another table? 查询问题:是否有更好的方法从一个表中选择所有记录,以及从另一表中选择不匹配的记录? - Query question: Is there a better way to select all records from one table, along with unmatched records from another table? SQL查询:选择表1中存在于表2中的所有记录+表2中存在于表1中不存在的所有记录 - SQL Query: select all the records from table1 which exist in table2 + all the records from table2 which don't exist in table1 SQL: select 其他查询未选择的所有记录 - SQL: select all records not selected by another query 如何从一张表中选择关系表中不存在的所有记录? - how to select all records from one table that not exist in relation table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM