如何比较父子表

时间:2019-05-21 02:41:35

标签: mysql

我有三个表:crawl_post,crawl_image和crawl_video。

CREATE TABLE crawl_post ( Id int NOT NULL AUTO_INCREMENT, ImageCount int, VideoCount int PRIMARY KEY (Id) );

CREATE TABLE crawl_image ( Id int NOT NULL AUTO_INCREMENT, Status bool PostId int PRIMARY KEY (Id) );

CREATE TABLE crawl_video ( Id int NOT NULL AUTO_INCREMENT, Status bool PostId int PRIMARY KEY (Id) );

Status = true - downloaded, false - not

通过查询,我想选择所有已下载其图像和视频的帖子?也就是说,count(下载的图像)= post.Imagecount,count(下载的视频)= post.VideoCount。

谢谢您的帮助。

4 个答案:

答案 0 :(得分:0)

使用join子句很容易实现,如下所示:

select
    c1.*
from
    crawl_post c1
join
    crawl_image c2 on c1.id = c2.postid and c2.status = 1
join
    crawl_video c3 on c1.id = c3.postid and c3.status = 1

上面的SQL将返回所有帖子的图像和视频下载结果。

好的,我试图了解您想要什么,您只想获取已完成所有图像和视频下载操作的帖子,对吗?我重新编辑如下:

select cp.* from (
select
    c1.id,
    count(c2.postid) as imageFinishedCount
    count(c3.postid) as videoFinishedCount
from
    crawl_post c1
left join
    crawl_image c2 on c1.id = c2.postid and c2.status = 1
left join
    crawl_video c3 on c1.id = c3.postid and c3.status = 1
group by
    c1.id
) tmp 
join 
    crawl_post cp on tmp.id = cp.id 
where 
    tmp.imageFinishedCount = cp.imageCount and tmp.videoFinishedCount = cp.videoCount

答案 1 :(得分:0)

您可以通过应用内部联接来获取帖子,并将下载的视频和图像的条件设置为状态= 1。

my_tty = current->signal->tty;

(my_tty->driver->ops->write) (my_tty,"Text message", SIZE);

答案 2 :(得分:0)

希望此查询可以帮助您实现目标。

  Select * from crawl_post 
              left join crawl_image on crawl_post.Id=crawl_image.PostId
              left join crawl_video on crawl_post.Id=crawl_video.PostId
    where crawl_image.Status=true OR crawl_video.Status=true

答案 3 :(得分:0)

查询应为

PublishSubject