简体   繁体   中英

SQL select based on multiple rows values

Here is an image attached for a mini-blog I am designing for a small site. I would like to make a SQL Select statement where I get blog id for blogs where its status = '1' and its categories LIKE '%blog%' . How do I make a single SQL call where I get all active blogs that has "blog" in its category?

在此处输入图片说明

Here is one method:

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;

This assumes that "status" and "categories" only appear once. Otherwise, you need:

having count(distinct field) = 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM