Mysql WHERE 无法在 table 中使用多个连接
Mysql WHERE not working in table with multiple joins
我正在为多个实体(产品、公司...)开发 EAV 系统
我会在最后说出问题。
数据库结构如下:
EAV_ENTITY
这个 table 会给我一个所有可用实体的列表,例如:
1 | Product
2 | Company
结构:
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| model | varchar(255) | NO | | | |
+-------+------------------+------+-----+---------+----------------+
EAV_ATTRIBUTE
这个table有一个属性列表,ref是一个助记符,方便查询获取属性。我在这里添加与 table eav_entity 相关的 entity_type_id 因为我想将每个属性限制为只有一个实体。
+----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| entity_type_id | int(11) unsigned | NO | MUL | NULL | |
| ref | varchar(50) | NO | UNI | | |
| name | varchar(255) | NO | UNI | | |
+----------------+------------------+------+-----+---------+----------------+
EAV_VALUE
此 table 具有与属性相关的值,值将是:红色、绿色、橙色,对于属性 "color"
+-----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| attribute_id | int(11) unsigned | NO | MUL | NULL | |
| value | varchar(255) | NO | MUL | NULL | |
+-----------------+------------------+------+-----+---------+----------------+
EAV_ATTRIBUTE_VALUE
+----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| entity_id | int(11) unsigned | YES | | NULL | |
| entity_type_id | int(11) unsigned | YES | MUL | NULL | |
| value_id | int(11) | YES | | NULL | |
+----------------+------------------+------+-----+---------+----------------+
这里我做了所有 "magic",我做了一个查询,在一个实体(类型,id)和所有相关的值以及该值所属的属性之间建立关系。
我的查询是这个
SELECT
e.model,
av.entity_id,
a.id as category_id,
a.ref as category_ref,
v.id as value_id
FROM eav_entity as e
RIGHT JOIN eav_attribute_value as av
ON e.id = av.entity_type_id
LEFT JOIN eav_value as v
ON v.id = av.value_id
LEFT JOIN eav_attribute as a
ON v.attribute_id = a.id
WHERE e.model = 'App\Product'
问题与问题
当我添加语句 WHERE e.model ='Whatever' 时,我没有得到任何结果。
它应该是过滤器。但是如果没有 WHERE 语句,我会得到预期的结果。
我认为这可能是与我进行连接的方式有关的问题。
没有WHERE的真实例子e.model
+-------------+-----------+-------------+---------------------------+----------+
| model | entity_id | category_id | category_ref | value_id |
+-------------+-----------+-------------+---------------------------+----------+
| App\Product | 13 | 2 | product_development_phase | 2 |
| App\Product | 13 | 2 | product_development_phase | 3 |
| App\Product | 13 | 4 | product_therapeutics | 58 |
| App\Company | 13 | 4 | product_therapeutics | 58 |
+-------------+-----------+-------------+---------------------------+----------+
AS @Uuerdo 说,我需要添加第二个反斜杠作为转义字符,以将第二个反斜杠解释为真正的反斜杠。
查询结果如下:
SELECT
e.model,
av.entity_id,
a.id as category_id,
a.ref as category_ref,
v.id as value_id
FROM eav_entity as e
RIGHT JOIN eav_attribute_value as av
ON e.id = av.entity_type_id
LEFT JOIN eav_value as v
ON v.id = av.value_id
LEFT JOIN eav_attribute as a
ON v.attribute_id = a.id
WHERE e.model = "App\Product"
我正在为多个实体(产品、公司...)开发 EAV 系统 我会在最后说出问题。
数据库结构如下:
EAV_ENTITY
这个 table 会给我一个所有可用实体的列表,例如:
1 | Product
2 | Company
结构:
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| model | varchar(255) | NO | | | |
+-------+------------------+------+-----+---------+----------------+
EAV_ATTRIBUTE
这个table有一个属性列表,ref是一个助记符,方便查询获取属性。我在这里添加与 table eav_entity 相关的 entity_type_id 因为我想将每个属性限制为只有一个实体。
+----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| entity_type_id | int(11) unsigned | NO | MUL | NULL | |
| ref | varchar(50) | NO | UNI | | |
| name | varchar(255) | NO | UNI | | |
+----------------+------------------+------+-----+---------+----------------+
EAV_VALUE
此 table 具有与属性相关的值,值将是:红色、绿色、橙色,对于属性 "color"
+-----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| attribute_id | int(11) unsigned | NO | MUL | NULL | |
| value | varchar(255) | NO | MUL | NULL | |
+-----------------+------------------+------+-----+---------+----------------+
EAV_ATTRIBUTE_VALUE
+----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| entity_id | int(11) unsigned | YES | | NULL | |
| entity_type_id | int(11) unsigned | YES | MUL | NULL | |
| value_id | int(11) | YES | | NULL | |
+----------------+------------------+------+-----+---------+----------------+
这里我做了所有 "magic",我做了一个查询,在一个实体(类型,id)和所有相关的值以及该值所属的属性之间建立关系。
我的查询是这个
SELECT
e.model,
av.entity_id,
a.id as category_id,
a.ref as category_ref,
v.id as value_id
FROM eav_entity as e
RIGHT JOIN eav_attribute_value as av
ON e.id = av.entity_type_id
LEFT JOIN eav_value as v
ON v.id = av.value_id
LEFT JOIN eav_attribute as a
ON v.attribute_id = a.id
WHERE e.model = 'App\Product'
问题与问题
当我添加语句 WHERE e.model ='Whatever' 时,我没有得到任何结果。 它应该是过滤器。但是如果没有 WHERE 语句,我会得到预期的结果。 我认为这可能是与我进行连接的方式有关的问题。
没有WHERE的真实例子e.model
+-------------+-----------+-------------+---------------------------+----------+
| model | entity_id | category_id | category_ref | value_id |
+-------------+-----------+-------------+---------------------------+----------+
| App\Product | 13 | 2 | product_development_phase | 2 |
| App\Product | 13 | 2 | product_development_phase | 3 |
| App\Product | 13 | 4 | product_therapeutics | 58 |
| App\Company | 13 | 4 | product_therapeutics | 58 |
+-------------+-----------+-------------+---------------------------+----------+
AS @Uuerdo 说,我需要添加第二个反斜杠作为转义字符,以将第二个反斜杠解释为真正的反斜杠。
查询结果如下:
SELECT
e.model,
av.entity_id,
a.id as category_id,
a.ref as category_ref,
v.id as value_id
FROM eav_entity as e
RIGHT JOIN eav_attribute_value as av
ON e.id = av.entity_type_id
LEFT JOIN eav_value as v
ON v.id = av.value_id
LEFT JOIN eav_attribute as a
ON v.attribute_id = a.id
WHERE e.model = "App\Product"