SQL选择基于多行值

时间:2015-09-15 11:43:24

标签: php mysql sql select

这是我为小型网站设计的迷你博客附带的图片。我想创建一个SQL Select语句,其中我获得博客的博客ID,其status = '1'及其categories LIKE '%blog%'。如何进行单个SQL调用,我将获得所有活动的博客,其中包含"博客"在其类别?

enter image description here

1 个答案:

答案 0 :(得分:2)

这是一种方法:

select b.blog_id
from blogs b
where (b.field = 'status' and b.data = '1') or
      (b.field = 'categories' and b.data LIKE '%blog%')
group by b.blog_id
having count(*) = 2;

这假设"状态"和"类别"只出现一次。否则,你需要:

having count(distinct field) = 2