mysql 查询:同一列中匹配的几个条件
我的 mysql 数据库中有一个名为“events”的表:
+-----+-----------+------------------------------+------------+
| ID | CATEGORY | NAME | TYPE |
+-----+-----------+------------------------------+------------+
| 1 | 1 | Concert | music |
| 2 | 2 | Basketball match | indoors |
| 3 | 1 | Theather play | outdoors |
| 4 | 1 | Concert | outdoors |
+-----+-----------+------------------------------+------------+
我需要一个查询来计算类别 1 的事件以及哪种类型是音乐以及户外活动 这意味着从上表中计数应该仅为 1:存在 3 个类别为 1 的事件 但只有“音乐会”具有户外和音乐类型(ID 1 和 ID 4)。
该查询会是什么?可以吗?
I have this table named "events" in my mysql database:
+-----+-----------+------------------------------+------------+
| ID | CATEGORY | NAME | TYPE |
+-----+-----------+------------------------------+------------+
| 1 | 1 | Concert | music |
| 2 | 2 | Basketball match | indoors |
| 3 | 1 | Theather play | outdoors |
| 4 | 1 | Concert | outdoors |
+-----+-----------+------------------------------+------------+
I need a query to count the events with category 1 and which type is music and also outdoors
Meaning that from the table above the count should be only 1: there are three events with category 1
but only "Concert" has type outdoor and music (ID 1 and ID 4).
What would be that query? Can that be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
试试这个:
或者一种更难理解的方法,但比前一种方法快得多:
Try this:
Or a harder to understand way, but way faster than the previous one:
对于两个标准,我会使用阿林的答案。您可以使用下面的方法来获得更多数量。
For 2 criteria I would use Alin's answer. An approach you can use for greater numbers is below.
尝试这个查询
Try this query
尝试:
try:
一根线不断重置我的连接。与?我会尝试发布评论
one line keeps resetting my connection. wth? i'll try posting as a comment
从类别 = '1' 的事件中选择计数(不同 ID)作为 'eventcount' 并输入 ('music','outdoor')
Select count(distinct ID) as 'eventcount' from events where Category = '1' and Type in('music','outdoor')