我目前有三個表:的MySQL/MariaDB的 - 通過查詢參考表表搜索
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實際上是我已經與創建參考表:
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
);
我試圖通過搜索相應的標籤來篩選表格產品中的項目。 我已經找到了一些similar problems,但我無法讓它正常工作....
非常感謝提前。
請在MySQL閱讀文檔。 Foreignkey引用不會自動連接相關的表。您必須使用JOIN啓用實際關係。 – mootmoot