sql - Selecting rows with references across tables in SQLite 3 -


i have sqlite photo/album database 3 tables:

albums

 id   name           hide --------------------------  1    holiday 2010   1  2    day trip       0 

photos

 id   file -----------------  1    photo1.jpg  2    photo2.jpg  3    photo3.jpg  4    photo4.jpg 

relation (connects photos albums)

 album   photo -----------------  1       1  1       2  2       3  2       1 

a photo can assigned zero, 1 or several albums. each album has column 'hide' indicates, whether photos of album should ignored.

i'm trying find select query returns photos not assigned album + photos in albums not hidden (i.e. have 'hide' value set 0).

i came query selects photos in visible albums, don't know how include photos not assigned album:

select file  photos, albums, relation  photos.id = relation.photo    , albums.id = relation.album    , albums.hide = 0 

this query returns:

photo1.jpg photo3.jpg 

however, required result be:

photo1.jpg photo3.jpg photo4.jpg 

the problem photo4.jpg not assigned album in relation table. know how solve this?

thank help!

first of query retursn photo 1 , 3, assume correct.

so can join , null values have no album join on.

select file photos left outer join (relation join albums on relation.album = albums.id)             on relation.photo = photos.id albums.hide = 0 or albums.id null 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -