选择属于多个类别的行 [英] Select row belonging to multiple categories

查看:72
本文介绍了选择属于多个类别的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2张桌子.第一个表充满了条目.第二张表定义了该条目所属的类别:

I've got 2 tables. The first table is full of entries. The second table defines what categories that entry belongs to:

表1:

entry_id | title
       1 | Entry1
       2 | Entry2
       3 | Entry3

表2

entry_id | cat_id
       1 | 233
       1 | 234
       1 | 678
       2 | 235
       2 | 453
       2 | 21
       3 | 234
       3 | 233

我正在尝试选择一个查询,以查询属于多个类别的所有帖子.例如,我想返回属于类别ID(233和234)的条目.尽管我不太确定,但我认为这需要一个子查询.任何人有帮助吗? :)

I'm trying to select an entry with a single query of all posts belonging to multiple categories. For example, I want to return the entries belonging to category ids, 233 and 234. I believe this needs a subquery although I'm not quite sure. Any help anybody? :)

推荐答案

尝试一下:

select * from entity e
where exists (select * from category c where c.entry_id=e.entry_id AND c.cat_id=233)
  and exists (select * from category c where c.entry_id=e.entry_id AND c.cat_id=234)

这将返回属于 233和234的行(无论如何,这就是我的阅读方式;我可能误解了属于多个类别"部分).

This returns rows that belong to both 233 and 234 (this is how I read your question, anyway; I may have misunderstood the "belonging to multiple categories" part).

这篇关于选择属于多个类别的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆