3

我有 2 个表:goodscats_goods(关系表)。

商品:id, name.

Cats_goods: good_id, cat_id.

如何只选择goods同时具有cat_id=4 和cat_id=24 的?

试过:

SELECT DISTINCT *
FROM `goods` AS `g`
JOIN `cats_goods` AS `cg` ON (`g`.`id` = `cg`.`good_id`)
WHERE (`cg`.`cat_id` = 24 AND `cg`.`cat_id` = 4)

查询给出 0 个结果!

UPD: 对于每个good有 1 行cats_goods= cat_id4 和 1 行cats_goods= cat_id24。我只需要选择goods两个条件都匹配的那些。

UPD2:

goods表结构:

CREATE TABLE IF NOT EXISTS `goods` (
  `id` int(11) NOT NULL auto_increment,
  `code` varchar(50) NOT NULL default '',
  `title` varchar(255) NOT NULL default '',
  `price` decimal(10,2) NOT NULL default '0.00',
  `file` varchar(50) NOT NULL default '',
  `preview` varchar(50) NOT NULL default '',
  `order` int(11) NOT NULL default '0',
  `selltype_id` int(11) NOT NULL default '0',
  `xml_date` varchar(50) NOT NULL default '',
  `invalid` tinyint(1) NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `order` (`order`),
  KEY `selltype_id` (`selltype_id`),
  KEY `code` (`code`),
  KEY `invalid` (`invalid`),
  KEY `xml_date` (`xml_date`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5014 ;

goods表数据:

(4964, '00000001731', 'gold 585', 10000.00, '', '', 0, 2, '', 0),
(4965, '00000001733', 'gold 585', 10000.00, '', '', 0, 2, '', 0),
(4966, '00000001769', 'gold 585', 8000.00, '', '', 0, 2, '', 0),
(4967, '00000001767', 'gold 585', 8000.00, '', '', 0, 2, '', 0),

cats_goods表结构:

CREATE TABLE IF NOT EXISTS `cats_goods` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `good_id` int(10) unsigned NOT NULL default '0',
  `cat_id` int(10) unsigned NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `good_id` (`good_id`,`cat_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=37530 ;

cats_goods表数据:

(37474, 4964, 24),
(37478, 4966, 24),
(37477, 4966, 4),
(37476, 4965, 24),
(37475, 4965, 4),
(37475, 4967, 4),

只能goods选择 4965 和 4966。

4

4 回答 4

2

试试这个:

SELECT DISTINCT *
FROM `goods` AS `g`
JOIN `cats_goods` AS `cg` ON (`g`.`id` = `cg`.`good_id`) 
                          and (`cg`.`cat_id` = 24 OR `cg`.`cat_id` = 4)

或者

SELECT DISTINCT *
FROM `goods` AS `g`
JOIN `cats_goods` AS `cg` ON (`g`.`id` = `cg`.`good_id`) 
where `g`.`id` in (24,4)

或者

  SELECT DISTINCT *
    FROM `goods` AS `g`
    JOIN `cats_goods` AS `cg1` ON (`g`.`id` = `cg1`.`good_id`) 
                               and (`cg1`.`cat_id` = 24) 
    JOIN `cats_goods` AS `cg2` ON (`g`.`id` = `cg2`.`good_id`) 
                               and (`cg2`.`cat_id` = 4)
于 2012-10-12T08:45:42.313 回答
2

尝试

SELECT DISTINCT *
FROM `goods` AS `g`
JOIN `cats_goods` AS `cg1` ON (`g`.`id` = `cg1`.`good_id`)
JOIN `cats_goods` AS `cg2` ON (`g`.`id` = `cg2`.`good_id`)
WHERE (`cg1`.`cat_id` = 24 AND `cg2`.`cat_id` = 4)
于 2012-10-12T08:52:00.387 回答
0

这个查询比较有趣。一个字段如何同时具有两个值

SELECT DISTINCT *
FROM `goods` AS `g`
JOIN `cats_goods` AS `cg` ON (`g`.`id` = `cg`.`good_id`)
WHERE (`cg`.`cat_id` = 24 AND `cg`.`cat_id` = 4)

OR把它WHERE改成条件,伙计。

SELECT DISTINCT *
FROM `goods` AS `g`
JOIN `cats_goods` AS `cg` ON (`g`.`id` = `cg`.`good_id`)
WHERE (`cg`.`cat_id` = 24 OR `cg`.`cat_id` = 4)

如果我问的是可能的,你能显示表格转储吗?

于 2012-10-12T08:43:34.653 回答
0
SELECT a.good_id, a.cat_id from cats_goods a inner join goods b on a.good_id=b.id where a.cat_id in (4,24);
于 2012-10-12T08:48:49.633 回答