简体   繁体   English

mySQL问题分组并使用GROUP BY / HAVING返回正确的值

[英]mySQL -problem grouping and returning correct values using GROUP BY / HAVING

I have a crossReference table (crossref_room_subject) with conference room, meeting subject and timeSlot IDs. 我有一个带有会议室,会议主题和时隙ID的crossReference表(crossref_room_subject)。 There are multiple entries per timeSlot. 每个时隙有多个条目。 The subjectCode table includes a subjectCodeCategory ID also (only one category per subject but multiple subjects per category). subjectCode表还包含一个subjectCodeCategory ID(每个主题仅一个类别,但每个类别多个)。 For one query that is working, the user is entering multiple subjectCodes and I need to return the timeSlots where all subjectCodes exist. 对于一个有效的查询,用户正在输入多个subjectCodes,我需要返回存在所有subjectCodes的时隙。 I am now getting good results (thanks again Sparky) using GROUP BY and HAVING but all the information needed for this query in the one table: crossref_room_subject. 我现在使用GROUP BY和HAVING得到了很好的结果(再次感谢Sparky),但是在一个表中此查询所需的所有信息为:crossref_room_subject。

I have one last scenario that uses more variables and I can't get it to work. 最后一种情况是使用更多变量,但无法正常工作。 This time the user enters multiple subjectCodeCategorys. 这次用户输入多个subjectCodeCategorys。 I have to link to the subjectCodes table to crossreference subjectCodeCategory with the subjectCode. 我必须链接到subjectCodes表,以将subjectCodeCategory与subjectCode交叉引用。 I have tried it a bunch of ways, mostly changing the SELECT and/or GROUP BY, this seems to be where I should be able to group those subjectCodeCategories. 我已经尝试了很多方法,主要是更改SELECT和/或GROUP BY,这似乎是我应该能够对这些subjectCodeCategories进行分组的地方。 But it is not working (see below). 但它不起作用(请参阅下文)。 I am getting returns for each timeSlot that has 3 returns--good except it only counts subjectCode, regardless of subjectCodeCategory. 我每有3个退货的timeSlot都会得到退货-很好,除了它仅计入subjectCode,而与subjectCodeCategory无关。 I need to return only those timeSlots where one (or more) of each of the three subjectCodeCategories is represented. 我只需要返回代表三个subjectCodeCategories中的每一个(或多个)的那些时隙。 I have tried adding subjectCodeCategory to the SELECT and/or GROUP BY clauses but don't get any better results. 我尝试将subjectCodeCategory添加到SELECT和/或GROUP BY子句中,但是没有得到更好的结果。

Thanks for any help or ideas in advance. 感谢您的任何帮助或提前的想法。 Grey 灰色

SELECT a.meetingID, a.timeSlot FROM crossref_room_subject a , subjectCodes b WHERE a.subjectID = b.subjectID AND ( b.subjectCodeCategory =8 OR b.subjectCodeCategory =19 OR b.subjectCodeCategory =23 ) GROUP BY a.meetingID, a.timeSlot HAVING COUNT( * ) =3 LIMIT 0 , 30 从crossref_room_subject a中选择a.meetingID,a.timeSlot a,subjectCodes b,其中a.subjectID = b.subjectID AND((b.subjectCodeCategory = 8或b.subjectCodeCategory = 19或b.subjectCodeCategory = 23)GROUP BY a.meetingID,a。时隙HAVING COUNT(*)= 3 LIMIT 0,30

I figured it out. 我想到了。 This gives the results I need: 这给出了我需要的结果:

SELECT a.meetingID, a.timeSlot 
FROM crossref_room_subject a , subjectCodes b 
WHERE a.subjectID = b.subjectID 
AND ( b.subjectCodeCategory =8 OR b.subjectCodeCategory =19 OR b.subjectCodeCategory =23 ) 
GROUP BY a.meetingID, a.timeSlot 
HAVING COUNT( DISTINCT b.subjectCodeCategory ) =3 LIMIT 0 , 30

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM