我需要帮助构建MYSQL查询:
主表列出了待售车辆的人名和地址。
属性(第二个)表列出车辆属性。
如何计算MAIN TABLE中卖家的数量,其中属性表COLOR为RED,YEAR为2015,PRICE小于15,000且客户地址为Columbus?
select count(`master_id`)
from `customers` c
left join `attributes` a
on c.master_id = a.master_id
where (
a.name = 'color' and a.value = 'red' and
a.name = 'year' and a.value = '2015' and
a.name = 'price' and a.value > '15000' and
c.city = 'Columbus';
答案 0 :(得分:0)
您可以使用token ws { <!ww> \s* }
和-S
:
$> PYTHONPATH=$HOME/src:/usr/lib/python2.7/site-packages python -S foo/test.py
请注意,这使用元组来简化group by
子句。
答案 1 :(得分:0)
您可以为每个属性反复加入表格。这使得连接更复杂,但WHERE
子句更简单。此查询基于数据存储的假设,因为它在问题中不可用。
SELECT COUNT(c.*)
FROM `customers` c
LEFT JOIN `attributes` aclr
ON aclr.name = 'color'
AND c.master_id = aclr.master_id
LEFT JOIN `attributes` ayr
ON ayr.name = 'year'
AND c.master_id = ayr.master_id
LEFT JOIN `attributes` aprc
ON aprc.name = 'price'
AND c.master_id = aprc.master_id
WHERE aclr.value = 'red'
AND ayr.value = '2015'
AND aprc.value > '15000'
AND c.city = 'Columbus'
GROUP BY c.master_id;