Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine two tables in SQLite

Tags:

join

sqlite

I have two tables, ta and tb:

ta:

key col1  
--------
k1 a 
k2 c 

tb:

key col2  
-------
k2 cc 
k3 ee 

They connected by "key". I want to know how can I get a table, tc, like:

key col1 col2  
-------------
k1 a  
k2 c cc 
k3  ee

Is there a easy method instead of inserting every record? They are one million records of tables so I need an effective way.

like image 503
trudger Avatar asked Jan 04 '10 10:01

trudger


People also ask

How can I join more than two tables in SQLite?

To query data from multiple tables, you use INNER JOIN clause. The INNER JOIN clause combines columns from correlated tables. Suppose you have two tables: A and B. A has a1, a2, and f columns.

How do I inner join in SQLite?

INNER JOIN creates a new result table by combining column values of two tables (table1 and table2) based upon the join-predicate. The query compares each row of table1 with each row of table2 to find all pairs of rows which satisfy the join-predicate.

Does SQLite support join?

SQLite supports different types of SQL Joins, like INNER JOIN, LEFT OUTER JOIN, and CROSS JOIN. Each type of JOIN is used for a different situation as we will see in this tutorial.

What is SQLite cross join?

It is a kind of join that results in the combination of all rows from the first table with all rows in the second. In SQLite, the CROSS JOIN produced a result set which is the product of rows of two associated tables when no WHERE clause is used with CROSS JOIN.


1 Answers

Make a VIEW of the two tables. Write a SELECT ... JOIN statement that gives you the result you want, and then use that as the base for a VIEW.

Example:

CREATE VIEW
  database.viewname
AS
  SELECT
    ta.key, 
    ta.col1,
    tb.col2
  FROM
    ta
   LEFT JOIN
    tb
   USING(key)
like image 58
Emil Vikström Avatar answered Sep 23 '22 14:09

Emil Vikström