CREATE TABLE IF NOT EXISTS `blogs` (
`id` int(11) NOT NULL auto_increment,
`title` text collate utf8_bin NOT NULL,
`content` longtext collate utf8_bin NOT NULL,
`active` tinyint(4) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=2768 ;
CREATE TABLE IF NOT EXISTS `pics` (
`id` int(11) NOT NULL auto_increment,
`blogid` int(11) NOT NULL default '0',
`islogo` tinyint(4) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=4132 ;
CREATE TABLE IF NOT EXISTS `vdos` (
`id` int(11) NOT NULL auto_increment,
`blogid` int(11) NOT NULL default '0',
`file` varchar(255) collate utf8_bin NOT NULL,
`title` varchar(255) collate utf8_bin NOT NULL,
`description` text collate utf8_bin NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=3759 ;
select distinct b.id from blogs b
left join pics p ON b.id = p.blogid
left join vdos v ON b.id = v.blogid
where p.islogo = '0' and b.`active` = '1'
我打算做的是列出包含图片或视频的博客ID。这个查询的作用是它只列出有图片的博客,而不列出只有视频的博客ID。
谁能看到我做错了什么?
答案 0 :(得分:3)
那是因为你设置了pics.islogo为'0'的条件。没有图片的博客永远不会是'0'。将条件移动到连接:
select distinct b.id from blogs b
left join pics p ON b.id = p.blogid and p.islogo = '0'
left join vdos v ON b.id = v.blogid
where b.`active` = '1'
答案 1 :(得分:1)
p.islogo只会导致带有图片的博客。你必须要做
where p.islogo = '0' and b.`active` = '1' or p.islogo IS NULL
还要匹配没有图片的博客。
编辑: 抱歉,最初误读了这个问题。 where子句应该更改为
WHERE (p.islogo = "0" AND p.id IS NOT NULL) OR (v.id IS NOT NULL)
答案 2 :(得分:-1)
select from blogs b
left join pics p ON b.id = p.blogid
left join vdos v ON b.id = v.blogid
where p.islogo = '0' and b.`active` = '1' GROUP BY b.id;