0

我有 3 张桌子要放在一起。一个包含所有用户视频的视频表,一个包含所有用户图像的图像表和一个摄像机表。我想列出所有图像和视频并对其进行排序

prefix_videos 表与(videoid, userid, cameraid, videosize, datetime, deleted, videolength, videoname)

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

prefix_cameras 表与(userid, cameraid, ...)

以前我只有视频表,所以这个查询给了我我需要的东西:

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 以及按日期排序这些条目的能力,无论它来自图像还是视频。

4

2 回答 2

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' 
于 2013-06-22T04:48:32.853 回答
0

简而言之,是的,您可以在 JOIN 上 JOIN 上 JOIN。语法通常看起来像

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

等等。我过于简单化了,但你明白了。

于 2013-06-22T03:18:21.710 回答