MySQL/MariaDB - 查询通过引用 table 在 table 中搜索
MySQL/MariaDB - query Searching in table via a reference table
我目前有三个 table:
desc products;
+----------------+-------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------------+------+-----+---------+----------------+
| id | int(255) unsigned | NO | PRI | NULL | auto_increment |
| name | text | NO | | NULL | |
| desc_short | text | NO | | NULL | |
+----------------+-------------------+------+-----+---------+----------------+
desc tags;
+------------+-------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------------+------+-----+---------+----------------+
| id | int(255) unsigned | NO | PRI | NULL | auto_increment |
| tag | varchar(255) | YES | UNI | NULL | |
| iscategory | tinyint(4) | NO | | 0 | |
+------------+-------------------+------+-----+---------+----------------+
desc products_tags;
+------------+-------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------------+------+-----+---------+-------+
| product_id | int(255) unsigned | YES | MUL | NULL | |
| tag_id | int(255) unsigned | YES | MUL | NULL | |
+------------+-------------------+------+-----+---------+-------+
products_tags 实际上是一个引用 table,我用以下方法创建的:
CREATE TABLE product_tags (
product_id INT(255) UNSIGNED,
tag_id INT(255) UNSIGNED,
CONSTRAINT fk_products_id FOREIGN KEY (tag_id) REFERENCES tags(id) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT fk_tag_id FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE ON UPDATE CASCADE
);
我正在尝试通过搜索相应的标签来过滤 table 产品中的项目。
我已经找到了一些 similar problems 但我无法让它正常工作....
非常感谢。
试试这个连接查询:
SELECT p.id, p.name, p.desc_short
FROM products p
INNER JOIN products_tag pt
ON p.id = pt.product_id
INNER JOIN tags t
ON t.id = pt.tag_id
WHERE t.tag = 'some tag'
我目前有三个 table:
desc products;
+----------------+-------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------------+------+-----+---------+----------------+
| id | int(255) unsigned | NO | PRI | NULL | auto_increment |
| name | text | NO | | NULL | |
| desc_short | text | NO | | NULL | |
+----------------+-------------------+------+-----+---------+----------------+
desc tags;
+------------+-------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------------+------+-----+---------+----------------+
| id | int(255) unsigned | NO | PRI | NULL | auto_increment |
| tag | varchar(255) | YES | UNI | NULL | |
| iscategory | tinyint(4) | NO | | 0 | |
+------------+-------------------+------+-----+---------+----------------+
desc products_tags;
+------------+-------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------------+------+-----+---------+-------+
| product_id | int(255) unsigned | YES | MUL | NULL | |
| tag_id | int(255) unsigned | YES | MUL | NULL | |
+------------+-------------------+------+-----+---------+-------+
products_tags 实际上是一个引用 table,我用以下方法创建的:
CREATE TABLE product_tags (
product_id INT(255) UNSIGNED,
tag_id INT(255) UNSIGNED,
CONSTRAINT fk_products_id FOREIGN KEY (tag_id) REFERENCES tags(id) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT fk_tag_id FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE ON UPDATE CASCADE
);
我正在尝试通过搜索相应的标签来过滤 table 产品中的项目。 我已经找到了一些 similar problems 但我无法让它正常工作....
非常感谢。
试试这个连接查询:
SELECT p.id, p.name, p.desc_short
FROM products p
INNER JOIN products_tag pt
ON p.id = pt.product_id
INNER JOIN tags t
ON t.id = pt.tag_id
WHERE t.tag = 'some tag'