【发布时间】:2015-07-27 13:46:59
【问题描述】:
我有一个 Mysql 查询,请查看以下内容:
SELECT `tbl_classSubjects`.`classID` , `tbl_classSubjects`.`sectionID` ,
`tbl_classes`.`name` AS className, `tbl_sections`.`name` AS sectionName
FROM `tbl_classSubjects`
JOIN `tbl_classes` ON `tbl_classSubjects`.`classID` = `tbl_classes`.`classID`
JOIN `tbl_sections` ON `tbl_classSubjects`.`sectionID` = `tbl_sections`.`sectionID`
WHERE `tbl_classes`.`classID` IN ( 24, 24, 27, 28 )
AND tbl_sections.sectionID IN ( 19, 20, 19, 15 )
GROUP BY `tbl_classSubjects`.`classID` , `tbl_classSubjects`.`sectionID`
ORDER BY `className` , `sectionName`
LIMIT 0 , 30
产生的结果为:
classID sectionID className sectionName
27 19 6th A
27 20 6th B
27 15 6th C
28 19 7th A
28 20 7th B
28 15 7th C
24 19 LKG A
24 20 LKG B
这是错误的。我要映射
WHERE IN ( 24, 24, 27, 28 )
AND tbl_sections.sectionID IN ( 19, 20, 19, 15 )
as 24-19, 24-20, 27-19, 28-15 所以它产生的结果为
classID sectionID className sectionName
27 19 6th A
28 15 7th C
24 19 LKG A
24 20 LKG B
【问题讨论】:
-
你为什么把 2 in () 中的一些数字加倍
-
您误解了
in的工作原理。它与您脑海中的组合不匹配。它只是说,在里面吗! -
你也在做一个没有聚合列的分组。这没什么大不了的,但它强制 DISTINCT 这很可能是你想要的(不会混淆下一个程序员)
-
是的,你是对的,IN 在组合中不匹配,但这是我实际需要的适当结果。有可能吗?
-
是的。请参阅下面的 2 个答案
标签: mysql where where-clause where-in