SQL对具有多对多关系的多个维度进行过滤

发布于 2024-11-07 05:07:01 字数 624 浏览 4 评论 0原文

在我的 Rails 应用程序中,我有三个表来处理课程和类别之间的多对多关系

  • 课程
  • course_categories_courses
  • course_categories

我有类别组,并且我希望允许通过以下界面按类别过滤课程列表:

位置

  • 非常近

类型

要搜索近或远的中等类型我曾想过使用:

SELECT distinct courses.* 
FROM `courses` 
  inner join course_categories on 
    course_categories_courses.course_category_id = course_categories.id 
    and (
      course_categories.id in ('medium') 
      and course_categories.id in ('near', 'far')
    )

但这不起作用。有人能指出我正确的方向吗?

In my rails app I have three tables to deal with the many-to-many relationship between courses and categories

  • courses
  • course_categories_courses
  • course_categories

I have groups of categories, and I want to allow filtering of the listing of the courses by categories through an interface like:

Location

  • very near
  • near
  • far

Type

  • short
  • medium
  • long

To search for medium types either near or far I had thought of using:

SELECT distinct courses.* 
FROM `courses` 
  inner join course_categories on 
    course_categories_courses.course_category_id = course_categories.id 
    and (
      course_categories.id in ('medium') 
      and course_categories.id in ('near', 'far')
    )

but that's not working. Anyone able to point me in the right direction please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感情废物 2024-11-14 05:07:01

您通常希望将“关系”作为连接条件,并将值(文字)放在 where 子句中进行过滤

。加入条件 - on course_categories_courses.course_category_id = course_categories.id

例如。 where 子句 - where course_categories.id in ('near', 'far')

所以您可能想以这种方式重写您的查询。但更重要的是,这两种情况怎么可能是真的呢?

(course_categories.id in ('medium') and course_categories.id in ('near', 'far'))

这就是你的意图吗?

You generally want to put to 'relationships' as join conditions, and values (literals) to filter by in the where clauses

ex. join condition - on course_categories_courses.course_category_id = course_categories.id

ex. where clause - where course_categories.id in ('near', 'far')

So you might want to rewrite your query that way. But more than that, how can the both of these possible be true?

(course_categories.id in ('medium') and course_categories.id in ('near', 'far'))

Is that what you intend?

一梦浮鱼 2024-11-14 05:07:01

您必须连接两个表,并且您正在使用 id 字段,其中应使用类型和位置(根据您的描述)

SELECT distinct courses.* FROM `courses` 
inner course_categories_courses on course_categories_courses.course_category_id = courses.course_category_id
inner join course_categories on 
course_categories.id = course_categories_courses.id
where  (course_categories.type in ('medium') and course_categories.location in ('near', 'far'))

You have to join both tables and you are using id field where type and location (according to your description) should be used

SELECT distinct courses.* FROM `courses` 
inner course_categories_courses on course_categories_courses.course_category_id = courses.course_category_id
inner join course_categories on 
course_categories.id = course_categories_courses.id
where  (course_categories.type in ('medium') and course_categories.location in ('near', 'far'))
北城半夏 2024-11-14 05:07:01

您编写的查询的语法很好。正如其他人指出的那样,它有两个不寻常的地方:

  1. “内部连接上的过滤器”。 (通常在 where 完成,但也许您有自己的原因)
  2. course_categories.id in ('medium')。大多数人会期望这里有一个数字,但这也许只是为了演示目的。

它“不起作用”的原因

  1. 您收到了一个错误,您已将其隐藏在红宝石中,表示您没有共享
  2. 它的工作原理,只是没有符合您条件的记录。

调试方法

  1. 在工作台(或其他客户端)中运行它并检查结果
  2. 如果没有错误并且没有返回记录,则在工作台中运行一些查询

    从 course_categories 中选择 count(*),其中 course_categories.id in ('medium')

    Select count(*) from course_categories where course_categories.id in ('near', 'far')

  3. 如果您在 Workbench 中得到了您想要的结果,那么它可能是您的客户端(Ruby)代码,也许是错误的连接字符串?

Syntactaclly the query you wrote is fine. There are two unusal things about it as other's have noted

  1. Filter on a Inner join. (Typically done in the where but perhaps you have your reasons)
  2. course_categories.id in ('medium'). Most would expect a number here but perhaps this is just for demonstration purposes.

The reason for it "not working"

  1. You're getting an error that you've swept under the rug in your ruby that you're not sharing
  2. Its working there simply are no records matching your criteria.

Ways to debug.

  1. Run it in Workbench (or some other client) and check the results
  2. If there are no errors and no record are returned run some queries in workbench

    Select count(*) from course_categories where course_categories.id in ('medium')

    Select count(*) from course_categories where course_categories.id in ('near', 'far')

  3. If you got the results you wanted in Workbench then its probably your client (Ruby) code, bad connection string perhaps?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文