简体   繁体   English

如何在 SQLITE 中从一个表中获取所有列并从另一表中获取一列

[英]How to get all columns from one table and one column from another table in SQLITE

I have 2 tables with the same column names but with different data.我有 2 个具有相同列名但具有不同数据的表。 Consider the following:考虑以下:

Table1 Structure:表1结构:

id, sid, text
1,  1,   'Hello World'
2,  1,   'Hello World again'
3,  2,   'Hw...'
4,  2,   'Hw2...'

And Table2:和表2:

id, sid, text
1,  1,   'Hello World 2'
2,  1,   'Hello World again 2'
3,  2,   'Hw2...'
4,  2,   'Hw22...'

I want to query "Table1" based on "sid".我想根据“sid”查询“Table1”。 For example, if sid is 1 then I should get 2 rows as a result with all columns from "Table1" but just column "text" from "Table2".例如,如果 sid 为 1,那么我应该得到 2 行结果,其中包含“Table1”中的所有列,但只有“Table2”中的“text”列。 Like this:像这样:

id, sid, text,                  text
1,  1,   'Hello World',        'Hello World2'
2,  1,   'Hello World again',  'Hello World again 2'

I am trying to INNER JOIN But I get repition:我正在尝试 INNER JOIN 但我得到了重复:

SELECT Table1.*, Table2.text FROM Table1 INNER JOIN Table2 ON Table1.sid = Table2.sid WHERE Table1.sid = 1;

But I am getting duplicate rows?但我得到重复的行? How can I solve this.我该如何解决这个问题。 I know there are many similar questions but I could not find one with this problem.我知道有很多类似的问题,但我找不到有这个问题的问题。

You have to use sid and id to identify the right row您必须使用 sid 和 id 来识别正确的行

SELECT 
    t1.*, t2.text
FROM
    Table1 t1
        INNER JOIN
    Table2 t2 ON t1.sid = t2.sid AND t1.id = t2.id
WHERE
    Tt1.sid = 1;

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

相关问题 如何从一个表中获取所有列,而从另一张表中仅获取具有ID的一列? -MySQL - How to get all columns from one table and only one column from another table with ID ? - MySql 从一个表中获取一列,而从另一表中获取两列 - Get one column from one table with two columns of another table 如何将一列连接到另一个表中的所有列 - how to join one column to all columns from another table 如何将一个列数据从一个表中获取到另一个表中作为PL / SQL中的不同列 - how to get one column data from one table into another table as different columns in PL/SQL 获取具有一列的一个表的所有列,该列必须从与其数据对应的另一个表派生数据 - Get all Columns of One table having one column which has to derive data from another table corresponding to its data 从一个表到另一表获取列 - Get columns from one table to another Select 一个表的所有列和另一个表的一些列 - Select all columns from one table and some from another table 返回表1中的所有列,表2中的一列 - Return all columns from table 1, and one column from table 2 SQL选择一个表中的所有列和另一个表上另一列的最大值 - SQL Select all columns from one table and Max value of another column on another table SQL查询可从一个表中检索所有列,而从另一表中检索一个列 - SQL Query that retrieves all columns from one table and one column from the another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM