此问题与我在schema中就库存控制应用程序建议的original question有关。
我正在尝试创建一个MySQL查询,为特定项目提供当前库存。
查询正在运行,但我想知道是否有更有效的方法来获取我需要的信息。
SELECT 's'.*,
'v1'.'attribute_id' AS 'att1',
'v1'.'value' AS 'val1'
'v2'.'attribute_id' AS 'att2',
'v2'.'value' AS 'val2'
FROM 'eav_ev' AS 'ev1'
INNER JOIN 'stock' AS 's' ON s.id = ev1.stock_id
INNER JOIN 'eav_ev' AS 'ev2' ON ev1.stock_id = ev2.stock_id
INNER JOIN 'eav_value' AS 'v1' ON v1.id = ev1.value_id
INNER JOIN 'eav_value' AS 'v2' ON v2.id = ev2.value_id
WHERE (ev1.entity_id = '45')
AND (ev1.value_id <> ev2.value_id)
AND (s.total > 0)
GROUP BY 'ev1'.'stock_id'
ORDER BY 'ev1'.'value_id' ASC
这会返回
的内容array (1) {
[0] => array(5) {
["stock_id"] => "2"
["att1"] => "3"
["val1"] => "M12"
["att2"] => "4"
["val2"] => "45"
}
}
看起来非常混乱,但我的大脑无法想出更好的东西。
有什么建议吗?
答案 0 :(得分:0)
如果先存储属性列表,则可以使用attribute_id AS att1
,而不是使用value AS attribute_X
。您可以简单地缓存查询,然后您可以在1个清晰查询中选择所有需要的数据。
假设您首先获取了属性ID列表(即SELECT attribute_id FROM eav_value
),请选择此项:
SELECT
v1.value_id AS attribute_1 -- (or whatever the ID was fetched in the first query)
v2.value_id AS attribute_2 -- (or whatever the second ID was fetched in the first query)
...
答案 1 :(得分:0)
经过一番搜索,我发现了以下问题/文章。
Zend Select with self join overwriting fields
我想这可以回答我自己的问题。看起来有必要“发布”查询以正确分配属性。相当容易做,但看起来很乱。