[英]SQL query on a JOINED table with multiple conditions
I have two sql tables: The wall table and the tag table.我有两个 sql 表:墙表和标签表。 Each of them is linked with has_and_belongs_to_many relationship .
它们中的每一个都与has_and_belongs_to_many 关系相关联。 Also the tag table has unique names.
标签表也有唯一的名称。
Here are the tables in sql这是sql中的表
mysql> describe tags;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | UNI | NULL | |
| count | int(11) | NO | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
mysql> describe tags_walls;
+---------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------+------+-----+---------+-------+
| tag_id | bigint(20) | NO | MUL | NULL | |
| wall_id | bigint(20) | NO | | NULL | |
+---------+------------+------+-----+---------+-------+
mysql> describe walls;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
I am in rails 5 and i want to query a wall that has multiple tags.我在 rails 5 中,我想查询具有多个标签的墙。
I'm trying to do我正在尝试做
result = Wall.all.includes(:tags).where(tags: {name: 'TAG1'})
result = result.where(tags: {name: 'TAG2'})
and the query that is constructed by rails is由 rails 构造的查询是
SELECT DISTINCT `walls`.`id`
FROM `walls`
LEFT OUTER JOIN `tags_walls` ON `tags_walls`.`wall_id` = `walls`.`id`
LEFT OUTER JOIN `tags` ON `tags`.`id` = `tags_walls`.`tag_id`
WHERE `tags`.`name` = 'TAG1' AND `tags`.`name` = 'TAG2'
It should give me multiple walls as a results but the return is #<ActiveRecord::Relation []>
结果应该给我多堵墙,但返回是
#<ActiveRecord::Relation []>
I want to build a custom sql query and just do a我想构建一个自定义的 sql 查询,然后做一个
Wall.includes(:tags).where query
How can i do a WHERE query on a joined table with multiple conditions linked by an AND ?如何对具有多个由 AND 链接的条件的连接表执行 WHERE 查询?
I would write this as:我会这样写:
SELECT tw.id
FROM tags_walls tw JOIN
tags t
ON t.id = tw.tag_id
WHERE t.name IN ('TAG1', 'TAG2')
GROUP BY tw.id
HAVING COUNT(*) = 2;
This assumes that tags are not duplicated on a wall.这假设标签没有在墙上重复。 If that is possible, then use
COUNT(DISTINCT t.name) = 2
.如果可能,请使用
COUNT(DISTINCT t.name) = 2
。
Notes:笔记:
walls
is not needed, so that JOIN
is removed.walls
,因此删除了JOIN
。INNER JOIN
is more appropriate than LEFT JOIN
.INNER JOIN
比LEFT JOIN
更合适。WHERE tags.name = 'TAG1' OR tags.name = 'TAG2'
或者
WHERE tags.name IN ('TAG1','TAG2')
SELECT w.id
FROM walls w
JOIN tags_walls tw
ON tw.wall_id = w.id
JOIN tags t
ON t.id = tw.tag_id
AND t.name IN('TAG1','TAG2')
GROUP
BY w.id
HAVING COUNT(DISTINCT t.name) = 2 -- where '2' equals the number of arguments in IN()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.