Mysql 选择在第二个表中有2个条件的具有联接的行
我有两个表:Mysql 选择在第二个表中有2个条件的具有联接的行,mysql,sql,join,Mysql,Sql,Join,我有两个表:goods和cats\u goods(关系表) 商品:id,name 猫货:good\u id,cat\u id 如何仅选择同时具有类别id=4和类别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
goods
和cats\u goods
(关系表)
商品:id
,name
猫货:good\u id
,cat\u id
如何仅选择同时具有类别id
=4和类别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
,在cats\u-goods
中有一行cats\u-id
=4,在cats\u-goods
中有一行cats\u-id
=24。我只需要选择两个条件都匹配的商品
UPD2:
货物
表格结构:
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 ;
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 ;
货物
表格数据:
(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),
(37474, 4964, 24),
(37478, 4966, 24),
(37477, 4966, 4),
(37476, 4965, 24),
(37475, 4965, 4),
(37475, 4967, 4),
cats\u货物
表格结构:
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 ;
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\u货物
表格数据:
(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),
(37474, 4964, 24),
(37478, 4966, 24),
(37477, 4966, 4),
(37476, 4965, 24),
(37475, 4965, 4),
(37475, 4967, 4),
只有
商品
4965和4966必须被选择。这个查询相当有趣。一个字段如何同时具有两个值
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)
在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)
如果我所问的是可能的,你能显示表转储吗?试试这个:
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)
或
或
试一试
不是选民,而是和其他答案一样的原因。这个问题很有道理,你只是给出了一个糟糕的答案,没有理由粗鲁。我认为问题很清楚(同时选择第1类商品和第2类商品)我理解这一点。但价值可以真正拥有它,但只有通过关系。我的意思是,对于每个good
,cats\u-goods
中有一行cats\u-id
=4,一行cats\u-goods
中有一行cats\u-id
=24Hey,@RaphaëlAlthaus,让我知道,一个值变量怎么可能同时包含两个值?这不是很愚蠢吗?(cg
cat\u id
=24和cg
cat\u id
=4)@webband您能否显示表格结构以获得更好的答案?我是DBA。这就是原因,我想帮助你,也想了解和了解你正在尝试做什么。或
不能让我的工作正常进行或
选择具有类别id
=24或类别id
=4的解析。我只需要选择两个条件都匹配的记录。是的,它也可以工作。我必须再加入一次《猫的商品》
表格。好吧,你的样本数据并不那么有趣,因为所有案例都符合你的要求…@webbandit我在回答Nalaka526和我的解决方案时加入了sqlFiddle。它不会给出相同的结果,也不确定你真正想要的结果是什么…@RaphaëlAlthaus Nalaka526的解决方案对我的任务很有效。我刚刚在我的项目中实现了他的示例。正如我在问题中所说的,我只需要选择两个条件都匹配的商品记录。Nalaka526的例子就是这样做的,只给我那些记录,而不是加倍。无论如何,谢谢你抽出时间@韦伯:哦,没问题,只是您的示例查询中的独特*让我想到了另一个解决方案;)