简体   繁体   English

select 如何将一张表中的所有数据与另一张表中的记录匹配,先选数据。 全部在一个查询中

[英]How to select all data from one table and records from another table matching data in first selection. All in one query

I have two tables Messages and Files .我有两个表MessagesFiles The Files table references Messages through its message_id foreign key which corresponds to the Messages 's primary key named message_id as well. Files表通过其message_id外键引用Messages ,该外键也对应于Messages的名为message_id的主键。 A message entity may or may not have a file but there cannot be a file without a message.消息实体可能有也可能没有文件,但没有消息就不可能有文件。

So I want to select everything in one query.所以我想 select 一次查询中的所有内容。 All the messages and if there is a file for a message then select a file as well.所有的消息,如果有消息的文件,那么 select 也是一个文件。 As far as I understand the resulting query should look something like this:据我了解,生成的查询应如下所示:

select * from Messages union select * from Files where message_id = Messages.message_id

unfortunately this simple query is not valid.不幸的是,这个简单的查询无效。 I also tried using joins:我也试过使用连接:

select * from Messages 
left outer join Files on Messages.message_id = Files.message_id 
union 
select * from Files 
left outer join Messages on Messages.message_id = Files.message_id

but it gives me only those messages which have a file.但它只给我那些有文件的消息。 Using subqueries doesn't seem to be a solution.使用子查询似乎不是解决方案。 So how do I do this?那么我该怎么做呢?

You want a full outer join here, which SQLite does not directly support.你想要一个完整的外部连接,SQLite 不直接支持。 You may emulate it with a union, along the lines of what you have already tried:您可以按照您已经尝试过的方式使用联合来模拟它:

SELECT m.*, f.*
FROM Messages m
LEFT JOIN Files f ON m.message_id = f.message_id
UNION ALL
SELECT m.*, f.*
FROM Files f
LEFT JOIN Messages m ON m.message_id = f.message_id
WHERE m.message_id IS NULL;

The key point of your requirement is:您的要求的关键点是:

A message entity may or may not have a file but there cannot be a file without a message消息实体可能有也可能没有文件,但没有消息就不可能有文件

which is the definition of a LEFT join of Messages to Files :这是Messages to FilesLEFT join 的定义:

SELECT * 
FROM Messages m LEFT JOIN Files f
ON f.message_id = m.message_id;

If you want data from one table and, if it exists, data from another table then why does a simple left outer join not work?如果您想要一个表中的数据,如果它存在,另一个表中的数据那么为什么简单的左外连接不起作用?

select * from Messages 
left outer join Files on 
Messages.message_id = Files.message_id

暂无
暂无

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

相关问题 SQL查询-从一个表中选择全部,在另一个表中匹配记录 - SQL Query - select all from one table with matching records in another 显示一个表中的所有记录,并显示另一个表中的匹配记录 - displaying all records from one table and matching records from another 查询问题:是否有更好的方法从一个表中选择所有记录,以及从另一表中选择不匹配的记录? - Query question: Is there a better way to select all records from one table, along with unmatched records from another table? Select 基于条件的一个表中的所有记录,而不是另一个表中的所有记录 - Select all records from one table not in another table based on a condition 如何用一次查询从另一张表的数据更新一张表的所有行字段? - How to updated all row's field of one table from data of another table with one time query? 如何 select 一个表中的所有记录在另一个表中不存在于另一个表中的某些条件下? - How to select all records from one table that do not exist in another table for certain condition in another table? 选择查询以将联结表中的所有数据获取到一个字段 - Select query to get all data from junction table to one field 如何将 Oracle 中一张表的所有数据传输到另一张表? - How to transfer all data from one table to another table in Oracle? 从一个查询中的另一个表中获取实体的所有数据 - Get all data for an entity from another table in one query 如何从一张表中选择关系表中不存在的所有记录? - how to select all records from one table that not exist in relation table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM