2013-06-22 74 views
0

我有3張桌子我想放在一起。包含所有用戶視頻的視頻表,包含所有用戶圖像的圖像表和攝像機表。我想列出所有的圖片和視頻,並與(videoid, userid, cameraid, videosize, datetime, deleted, videolength, videoname)加入3張表排序

prefix_images表(imageid, userid, cameraid, imagesize, datetime, deleted, imagename)

prefix_cameras表(userid, cameraid, ...)

對它們進行排序

prefix_videos表以前我只用了視頻表,這個查詢了我什麼我需要:

SELECT prefix_videos.videoid, prefix_videos.videoname, prefix_videos.videosize, 
prefix_videos.videodatetime, prefix_videos.videolength, prefix_cameras.cameraname FROM 
prefix_videos INNER JOIN prefix_cameras USING (userid,cameraid) WHERE 
prefix_videos.deleted IS NULL AND prefix_videos.userid=xxx ORDER BY 
prefix_videos.datetime DESC 

但現在我正在添加一個圖像表。所以,我要的是建立一個表:

imageid/videoid, cameraid, imagesize/videosize, datetime (either from image or video table), videoname/imagename

因爲我現在結合了圖像和視頻表給出一個結果,我不知道其他INNER JOIN有道理嗎?

這裏的另一個重要組成部分是爲NULL刪除的條目,並通過無論是來自圖像或視頻的日期順序對這些能力的檢查。

回答

1

您可以分別創建兩個選擇適合視頻和圖像,然後用工會把他們聚在一起。凌亂的部分是,你必須經過排序AFAIK

select * from(
SELECT prefix_videos.videoid, prefix_videos.videoname, prefix_videos.videosize, 
    prefix_videos.videodatetime, prefix_videos.videolength, prefix_cameras.cameraname FROM 
    prefix_videos INNER JOIN prefix_cameras USING (userid,cameraid) WHERE 
    prefix_videos.deleted IS NULL AND prefix_videos.userid=xxx ORDER BY 
    prefix_videos.datetime DESC 
    union 
    SELECT prefix_images.imageid, prefix_images.imagename, prefix_images.imagesize, 
    prefix_images.imagesdatetime, prefix_images.videolength, prefix_cameras.cameraname FROM 
    prefix_images INNER JOIN prefix_cameras USING (userid,cameraid) WHERE 
    prefix_images.deleted IS NULL AND prefix_images.userid=xxx ORDER BY 
    prefix_images.datetime DESC 
)a order by 'mycolumn' 

select imageid/videoid, cameraid, imagesize/videosize, datetime (either from image or video table), videoname/imagename 

from 
(select imageid as 'imageid/videoid', cameraid ,imagesize as 'imagesize/videosize', datetime, imagename as 'videoname/imagename' from prefix_images where ... 
union 
select videoid as 'imageid/videoid', cameraid ,videosize as 'imagesize/videosize', datetime, videoname as 'videoname/imagename' from prefix_videos where ... 
)as temptable 
inner join whatever_your_table on temptable.selected_column=whatever_your_table .equivalent_column 
order by 'mycolumn' 
+0

這不適合我,我認爲是因爲列不一樣。所以我認爲這違反了聯盟的規則。仍然試圖找出它...... – Tom

+0

你可以使用cast()將不同的列轉換爲不同的類型。 – AdrianBR

0

總之,是的,你可以加入JOIN後加入。語法通常看起來像

select [stuff] from [tableA] 
join [tableB] 
on [tableA.column] = [tableB.column] 
join [tableC] 
on [tableAorTableB.column] = [tableC.column] 
where [conditions] 

等等。我簡單化了,但你明白了。

+0

啊,真正的問題是-should-我做了三路連接,不-can- I. AdrianBR在這裏有更好的答案。 – catfood