简体   繁体   English

表中其他具有相同值的行的计数

[英]Count of other rows in table with the same value

So I'm a bit new to mySQL and can figure out how to do this. 因此,我对mySQL有点陌生,可以弄清楚该怎么做。 I have a table with columns for worker, the building they work in, and the office they work in. 我有一张桌子,上面有工作人员的柱子,工作的建筑物以及工作的办公室。

I'm trying to make query that will return this table with an extra column saying how many people work in the same room. 我正在尝试进行查询,该查询将返回此表并带有额外的列,该列表示在同一房间中有多少人工作。 so in this example Paul and Tim both work in xxx1 and so the column would say 2 but for John it would say 1 and he's the only person in that room. 因此,在此示例中,Paul和Tim都在xxx1中工作,因此该列将说2,但对于John,它将说1,并且他是该房间中的唯一人员。

| worker | office building   | room number     |
------------------------------------------------
| john   | xxx               | 0               | 
| paul   | xxx               | 1               |
| tim    | xxx               | 1               |
| Dave   | yyy               | 0               |    
| Ty     | yyy               | 1               |    
| Luke   | yyy               | 1               |

Try this: 尝试这个:

SELECT t1.*,
  (SELECT COUNT(1)
  FROM mytable t2
  WHERE t2.`room number` = t1.`room number`
  )
FROM mytable t1

I advice you don't use field name with white space, use the underscore instead white space. 我建议您不要在字段名中使用空格,而应使用下划线代替空格。

These sort of problems are solved with analytic functions. 这类问题可以通过解析函数解决。

So have a look at here about analytic functions. 因此,在这里了解一下解析函数。

What you need here is to use count(*) over as follows; 您需要在此处使用count(*)如下:

select  t.*, count(1) OVER (PARTITION BY office_building, room_number) from t

EDIT 编辑

Sorry, just noticed you are working on MYSQL, seems like no analytical functions available on MYSQL 抱歉,刚注意到您正在使用MYSQL,似乎在MYSQL上没有可用的分析函数

So try this; 所以试试这个;

   select t.*, wcounts.worker_count 
   from t,
   (select office_building, room_number count(1) as worker_count
      from t
     group by office_building, room_number) wcounts
   where t.office_building = wcounts.office_building
   and t.room_number = wcounts.room_number

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

相关问题 用其他表中的特定值对表中的行进行计数 - Count rows in table with certain value in other table 选择并显示表中具有相同值的行数 - Selecting and displaying the count of rows with the same value in a table 根据同一表中其他行的值更新行 - Update rows based on value of other rows in same table Mysql 用同一表列的计数更新所有行值 - Mysql update all rows value with count of same table column 将列值更新为同一表中特定值的COUNT行 - Update a column value to the COUNT of rows for specific values in same table MySQL-如果同一表的其他行中不存在值,则更新行 - Mysql - Update row if value is not present in other rows of same table 我怎样才能 select 多行与同一个表中的其他行的计数在一个单独的列中具有它们的主键? - How can I select multiple rows with the count of other rows in the same table that has their primary keys in a separate column? 具有相同字段值的行数 - Count of rows with same field value 表的SELECT计数,其中列为CONNECT,但与其他相关列相关的其他行没有特定值 - SELECT count from table where column is CONNECT but other rows with related other column does not have a certain value 如何计算具有特定值的行(ID),如果MySQL中相同的ID具有其他值,该如何排除呢? - How to count rows (IDs) that have a specific value and exclude if the same ID has other value in MySQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM