选择JOIN行,第二个表上有2个条件

时间:2012-10-12 08:41:54

标签: mysql sql join

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

商品:idname

Cats_goods:good_idcat_id

如何选择只有goods = 4 AND cat_id = 24的cat_id

尝试过:

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: 对于每个goodcats_goods中有1行cat_id = 4,cats_goods中有一行cat_id = 24.我只需要选择goods符合条件的{1}}。

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 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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)

答案 2 :(得分: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)

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

答案 3 :(得分: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);