Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL/MariaDB-通过引用表在表中进行查询搜索_Mysql_Select_Reference_Foreign Keys_Mariadb - Fatal编程技术网

MySQL/MariaDB-通过引用表在表中进行查询搜索

MySQL/MariaDB-通过引用表在表中进行查询搜索,mysql,select,reference,foreign-keys,mariadb,Mysql,Select,Reference,Foreign Keys,Mariadb,我目前有三个表: desc products; +----------------+-------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------------+-------------------+------+-----+---------+---

我目前有三个表:

    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
);
我试图通过搜索相应的标记来从表products中筛选项目。 我已经找到了一些,但我不能让它正常工作

非常感谢。请尝试以下加入查询:

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'

非常感谢,这确实有效。你能告诉我为什么我要写p.*在我通常放表名的地方吗。(这在其他示例中首先让我感到困惑)。@derped:当连接发生时,必须告诉mysql显式目标表名和列名。由于表名可能很长,我们可能会将其替换为短别名(即产品p中的
或产品“p”
中更具体的
),请阅读mysql上的文档。Foreignkey引用不会自动连接相关表。您必须使用JOIN来启用实际关系。