SQL对具有多对多关系的多个维度进行过滤
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您通常希望将“关系”作为连接条件,并将值(文字)放在 where 子句中进行过滤
。加入条件 -
on course_categories_courses.course_category_id = course_categories.id
例如。 where 子句 -
where 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?
Is that what you intend?
您必须连接两个表,并且您正在使用 id 字段,其中应使用类型和位置(根据您的描述)
You have to join both tables and you are using id field where type and location (according to your description) should be used
您编写的查询的语法很好。正如其他人指出的那样,它有两个不寻常的地方:
course_categories.id in ('medium')
。大多数人会期望这里有一个数字,但这也许只是为了演示目的。它“不起作用”的原因
调试方法。
如果没有错误并且没有返回记录,则在工作台中运行一些查询
从 course_categories 中选择 count(*),其中 course_categories.id in ('medium')
Select count(*) from course_categories where course_categories.id in ('near', 'far')
如果您在 Workbench 中得到了您想要的结果,那么它可能是您的客户端(Ruby)代码,也许是错误的连接字符串?
Syntactaclly the query you wrote is fine. There are two unusal things about it as other's have noted
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"
Ways to debug.
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')
If you got the results you wanted in Workbench then its probably your client (Ruby) code, bad connection string perhaps?