简体   繁体   English

如何获取一列中值的数量计数?

[英]how to get the count of number of values in a column?

I have a table called tbl_company . 我有一个名为tbl_company的表。 It has the fields 它有领域

id  comp_name     followers
 1    abc         15,23,88,99
 2    bbc         1,10,66

In followers field i am saving the employees id of those who are following that company. 在关注者字段中,我正在保存关注该公司的人员的员工ID。 This works fine. 这很好。 In code behind, each company page, I want to display how many followers have them 在每个公司页面后面的代码中,我想显示有多少关注者

example in abc company page i want show 我要显示的abc company页面中的示例

No of people following is 4 跟随的人是4

example in bbc company page i want show 我想要显示的bbc company页面中的示例

No of people following is 3 跟随的人是3

like this i want to display.how i can get this count..... 像这样我想显示。我如何获得这个计数.....

Thanks 谢谢

Comma-separated lists in a database field are unwieldy and in general a really bad idea. 在数据库字段中用逗号分隔的列表很笨拙,通常这是一个非常糟糕的主意。 The main reason being it completely nullifies the reason to use a relational database in the first place (eg indexes, foreign keys, and joins). 主要原因是它完全使最初使用关系数据库的原因(例如索引,外键和联接)无效。

You should normalize your database so you instead have: 您应该规范化数据库,以便拥有:

tbl_company
--------------
id   comp_name
1    abc
2    bbc
company_followers
----------------------
company_id follower_id
1          15
1          23
1          88
1          99
2           1
2          10
2          66

Then you can simply execute the following SQL: 然后,您可以简单地执行以下SQL:

SELECT COUNT(1)
FROM company_followers
WHERE company_id = @company_id

If you're absolutely stuck with this schema, you should first complain to whoever wrote it if possible <g>, then the simplest solution is to return the whole darn string and count the commas in C# (and add 1 of course). 如果您绝对坚持使用此模式,则应该首先向可能编写该代码的人投诉<g>,然后最简单的解决方案是返回整个darn字符串并计算C#中的逗号(当然要加1)。

Here's TSQL version, 这是TSQL版本,

SELECT  ID, 
        Comp_name, 
        CASE WHEN LEN(followers) > 0
             THEN (LEN(followers) - LEN(REPLACE(followers, ',', '')) + 1)
             ELSE 0
        END as FollowerCount 
FROM    tableName

In MySQL MySQL

SELECT  ID, 
        Comp_name, 
        CASE WHEN CHAR_LENGTH(followers) > 0
             THEN (CHAR_LENGTH(followers) - CHAR_LENGTH(REPLACE(followers, ',', '')) + 1)
             ELSE 0
        END as FollowerCount 
FROM    tableName

This is not a good database design. 这不是一个好的数据库设计。 You must think twice using this sort of table structure. 使用这种表结构必须三思。 This can be further normalized. 这可以进一步标准化。 If you don't properly do this at the beginning it could cause problems later and is not expandable. 如果开始时操作不正确,可能会在以后引起问题,并且无法扩展。 So, it would be better if you could change the table structure. 因此,最好更改表结构。

The data will look like the following with new table structure. 具有新表结构的数据如下所示。

company table: 公司表:

id     comp_name
1      abc
2      bbc

company_follower table: company_follower表:

company_id     follower
1              15
1              23
1              88
1              99
2              1
2              10
2              66

Then your query for your result is, 那么您对结果的查询是

SELECT COUNT(company_id)
  FROM company_follower
  WHERE company_id = @company_id;

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

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