0

如何选择具有用户指定的所有属性的产品

例如:用户指定属性来搜索产品:41,2,4,6

MYSQL结构为:

表:报价

`id` int(11) NOT NULL auto_increment,

`title_pl` varchar(100) character set utf8 collate utf8_polish_ci NOT NULL,

表:offer_att

`id` int(11) NOT NULL auto_increment,
`offer_id` int(11) NOT NULL,
`att_id` int(11) NOT NULL,

表属性

`id` int(11) NOT NULL auto_increment,

`title_pl` varchar(255) character set utf8 collate utf8_polish_ci NOT NULL,
4

2 回答 2

0

这是你需要的吗?

SELECT *
FROM offer
WHERE id IN
    (SELECT offer_id
     FROM offer_att oa,
          att a
     WHERE a.title_pl IN (41,
                          2,
                          4,
                          6)
       AND a.id = oa.att_id);

PS是一个小提琴样本。

于 2013-04-09T11:17:35.603 回答
0

如果您只想获取同时具有所有 4 个属性(41、2、4、6)的商品,那么您应该使用下一个查询

SELECT
    o.id
    o.title_pl
FROM
    offer o INNER JOIN offer_att oa ON o.id = oa.offer_id AND oa.att_id IN (41,2,4,6)   
GROUP BY o.id
HAVING COUNT(DISTINCT oa.att_id) = 4

如果您想获取至少具有一种属性(41 或 2 或 4 或 6)的报价,请使用以下属性:

SELECT
    o.id
    o.title_pl
FROM
    offer o INNER JOIN offer_att oa ON o.id = oa.offer_id AND oa.att_id IN (41,2,4,6)   

为您的 SQL 查询生成参数的 PHP 代码:

<?php
$attributes = array(41, 2, 4, 6);
$count = count($attributes);
$list = implode(',', $attributes);
$query = '
    SELECT
        o.id
        o.title_pl
    FROM
        offer o INNER JOIN offer_att oa ON o.id = oa.offer_id AND oa.att_id IN (' . $list . ')  
';
$db->query($query);
?>
于 2013-04-09T11:20:46.873 回答