SQL joining issue - 1 table in multiple join missing an entry
I have a query that joins multiple tables.
QString str1 = "SELECT DISTINCT f.pk_file_ID, f.file_name"
" FROM"
" File_Properties f"
", Video v "
", Audio a"
", Transport_Stream ts"
", TS_Data tsd"
", Signal_Root sr"
", Test_Case tc"
" WHERE"
" v.fk_file_ID = f.pk_file_ID"
" AND a.fk_file_ID = f.pk_file_ID"
" AND ts.fk_file_ID = f.pk_file_ID"
" AND tsd.fk_file_ID = f.pk_file_ID"
" AND sr.fk_file_ID = f.pk_file_ID"
" AND tc.fk_file_ID = f.pk_file_ID" + clause;
My app uses comboboxes that allow a user to select video file criteria which is used to fetched and display file data in a table. The selected criteria is stored in the clause
variable and is used to retrieve the matching files in the database.
The problem here is that a video file can have 0 to many Audio and Video entries. If, for example, a file does not contain any Audio data, there is no entry made in the Audio table. Therefore, this join does not work for retrieving files that do not have an Audio/Vide开发者_JS百科o entry.
One way around this would be to make a blank record in the tables where a video file has no Audio/Video data. However, I don't want to have blank records in the tables.
Is there a better way to join the tables?
Thanks
Have a look at the Left outer Join. Values of the left (in this case file_properties
) table are always displayed, even when they have no corresponding record in the right (in this case audio
and video
) table.
精彩评论