在mysql中从内部查询访问外部别名
我正在尝试创建一个可以搜索的查询 在产品标签和/或产品属性值中 在表格方面,我有:在mysql中从内部查询访问外部别名,mysql,Mysql,我正在尝试创建一个可以搜索的查询 在产品标签和/或产品属性值中 在表格方面,我有: 产品 product_具有_属性(将产品绑定到属性名称和属性值) 属性值 属性名称(我的查询不需要) 我就快到了,我想我应该使用group_concat, 但我在子查询中遇到了一个条件 以下是我到目前为止得到的信息: select p.id, p.label, ( select group_concat( distinct label separator
- 产品
- product_具有_属性(将产品绑定到属性名称和属性值)
- 属性值
- 属性名称(我的查询不需要)
select
p.id,
p.label,
(
select
group_concat( distinct label separator ', ')
from (
select
v.label
from attribute_value v
inner join product_has_attribute h on h.attribute_value_id=v.id
) as ttt
) as attr_values
from product p
having
label like :search
or attr_values like :search
这并不太糟糕,因为它在产品标签中进行搜索,但此查询的问题在于它进行搜索
在所有属性值中,但我只想搜索当前产品的属性值,
所以我试了一下:
select
p.id,
p.label,
(
select
group_concat( distinct label separator ', ')
from (
select
v.label
from attribute_value v
inner join product_has_attribute h on h.attribute_value_id=v.id
where h.product_id=p.id
) as ttt
) as attr_values
from product p
having
label like :search
or attr_values like :search
但现在它说:“未知列p.id”
如何让我的查询同时在产品标签和产品属性中进行搜索?只需删除额外不必要的SELECT级别。MySQL应该允许从中的一个级别引用外部查询。如果将其埋得更深(两个或更多级别),则无法引用 使属性值成为一个相关的子查询。。。只需一个
选择
大概是这样的:
SELECT p.id
, p.label
, ( SELECT GROUP_CONCAT(DISTINCT v.label ORDER BY v.label SEPARATOR ', ')
FROM attribute_value v
JOIN product_has_attribute h
ON h.attribute_value_id = v.id
WHERE h.product_id = p.id
) AS attr_values
FROM product p
HAVING p.label LIKE :search
OR attr_values LIKE :search
你真棒。谢谢。