Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite - LEFT JOIN

Tags:

sqlite

I need to make a query like this:

SELECT table1.*, table2.column 
FROM table1 
LEFT JOIN table2 ON table1.column = table2.column

But it's not working. If I try the same query but replacing the first part -- >

SELECT table1.column, table2.column 
FROM table1 
LEFT JOIN table2 ON table1.column = table2.column

it works fine. I need to bring all the columns from table1. How can I make it without specifing all of them?

like image 995
Agustín Avatar asked Oct 28 '13 12:10

Agustín


People also ask

Does SQLite have left join?

Introduction to SQLite LEFT JOIN clause Similar to the INNER JOIN clause, the LEFT JOIN clause is an optional clause of the SELECT statement. You use the LEFT JOIN clause to query data from multiple related tables.

Is SQLite right join?

Unfortunately, SQLite does not support the RIGHT JOIN clause and also the FULL OUTER JOIN clause. However, you can easily emulate the FULL OUTER JOIN by using the LEFT JOIN clause.

Can you do JOINs in SQLite?

To query data from both artists and albums tables, you use can use an INNER JOIN , LEFT JOIN , or CROSS JOIN clause. Each join clause determines how SQLite uses data from one table to match with rows in another table. Note that SQLite doesn't directly support the RIGHT JOIN and FULL OUTER JOIN .

What is SQLite inner join?

In SQLite, the INNER JOIN selects all rows from both participating tables to appear in the result if and only if both tables meet the conditions specified in the ON clause. JOIN, CROSS JOIN, and INNER JOIN are syntactic equivalents. In standard SQL, they are not equivalent.


1 Answers

If you have the same column name in table1 and table2, here is the solution for not specifying all the column name of table1 :

SELECT table1.*, table2.column as my_column_name_no_in_table1
FROM table1 
LEFT JOIN table2 ON table1.column = table2.column

If the column names of table1 and table2 are all different, you can use :

SELECT table1.*, table2.*
FROM table1 
LEFT JOIN table2 ON table1.column = table2.column

But as said in the peterm's comment, it is not a good practice in production. Now, do as you want! ;)

like image 79
kmas Avatar answered Oct 22 '22 04:10

kmas