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