如何计算列中属于另一列中不同值的所有内边值

时间:2022-01-11 01:13:47

I have a mysql database table which has 4 columns: ID, Date, SessionID, UserID(irrelevant here). Let's call them A, B, C,

我有一个mysql数据库表,它有4列:ID、日期、SessionID、UserID(此处不相关)。我们叫它们A B C,

What I can't figure out is how to count how many sessions has been booked to specific date.

我搞不懂的是如何计算到特定日期预订了多少课程。

For example something like this

举个例子。

      A  |      B     |  C
      ---|------------|------    
       1 | 2016-02-12 |  1  | 
       2 | 2016-02-12 |  1  | 
       3 | 2016-02-13 |  1  | 
       4 | 2016-02-12 |  5  | 
       5 | 2016-02-12 |  5  | 
       6 | 2016-02-13 |  2  | 
       7 | 2016-02-18 |  2  | 
       8 | 2016-02-19 |  3  |  

So I want that my php code would output that for date 2016-02-12 I have 2 entries with value 1 and 2 entries with value 5. For date 2016-02-13 I have one entrie with value 1 and one with value 2.

我希望我的php代码输出日期为2016-02-12我有两个条目值为1,两个条目值为5。我有一个entrie值1,一个值2。

2 个解决方案

#1


2  

This should work as you need

这应该可以满足您的需要

SELECT B AS date,COUNT(C) AS sessions FROM table GROUP BY B,C

#2


1  

It's a simple GROUP BY.

这是一个简单的团体。

SELECT Date, SessionID, COUNT(*)
FROM yourTable
GROUP BY Date, SessionID

#1


2  

This should work as you need

这应该可以满足您的需要

SELECT B AS date,COUNT(C) AS sessions FROM table GROUP BY B,C

#2


1  

It's a simple GROUP BY.

这是一个简单的团体。

SELECT Date, SessionID, COUNT(*)
FROM yourTable
GROUP BY Date, SessionID