Join in Sqlite Intermediary table

Mr.Phronesis

I have this schema in my sqlite database

CREATE TABLE `CARS` (
    `ID`    INTEGER,
    `Name`  TEXT,
    PRIMARY KEY(ID)
);

CREATE TABLE `OWNERS` (
`ID`    INTEGER,
`Name`  TEXT,
PRIMARY KEY(ID)
);

And an intermediary table between OWNERS Table and CARS Table

CREATE TABLE `OwnerCars` (
    `OwnerId`   INTEGER,
    `CarId` INTEGER,
    PRIMARY KEY(OwnerId,CarId),
    FOREIGN KEY(`OwnerId`) REFERENCES `Owners`(`Id`),
    FOREIGN KEY(`CarId`) REFERENCES `Cars`(`Id`)
);

Querying this i'll get only owners that have cars and their car count

select ownerid, count(carid) as carscount from OwnerCars 
                            inner join Owners on ownerid = id
                            group by(ownerid) 

How can i get all owners (including those without cars and their car count as 0). Thanks for any help!

DJo

Use Left Outer Join to your query, also keep the owner table to the left.

select o.id, count(carid) as carscount 
from owner o
Left Outer join OwnerCars on ownerid = o.id
group by(o.id) 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQLite: Multiple Table Join

From Dev

Intermediary Table Design

From Dev

Copying into Intermediary table not working

From Dev

Delete from SQLite table with join to link table

From Dev

SQLite left join with two conditions in right table

From Dev

Join a query result set with an existing table in sqlite?

From Dev

Sqlite - Multiple INNER JOIN on same Table with Limit

From Dev

SQlite Error: no such table: while using join

From Dev

Multi table join with SQLite UPDATE statement

From Dev

How delete table inner join with other table in Sqlite?

From Dev

How delete table inner join with other table in Sqlite?

From Dev

sqlite3: Preserve table names in query with JOIN

From Dev

Perform JOIN in SQLITE on two SELECT statements from the same table

From Dev

How can I store a 'join' query as a new table in SQLite?

From Dev

How do I select entries through an intermediary table relationship?

From Dev

How to fake migrations for not to create a specific existing intermediary table

From Dev

Hibernate : Update many-tomany intermediary table on Delete

From Dev

How do I select entries through an intermediary table relationship?

From Dev

Why do my 3 INNER JOIN on same table with different group and count do not work with sqlite?

From Dev

triple join using sqlite

From Dev

Sqlite merge columns in join

From Dev

SQLite - LEFT JOIN

From Dev

Is there join number limitation in SQLite?

From Dev

Trouble with a join statement in SQLite

From Dev

SQlite delete inner join

From Dev

SQLite Inner Join Issue

From Dev

Is there join number limitation in SQLite?

From Dev

SQLite Concat and JOIN of records

From Dev

Trouble with a join statement in SQLite

Related Related

HotTag

Archive