【问题标题】:Get Count of different values in comma separated row in mysql获取mysql中逗号分隔行中不同值的计数
【发布时间】:2015-01-25 17:32:27
【问题描述】:

一个表 Jobs 有 2 列 JobId, City 当我们保存工作时,工作位置可能是多个城市,如下所示

-----------------------------
JobId               City
-------------------------------
1                   New York
2                   New York , Ohio , Virginia
3                   New York , Virginia

我如何计算特定城市的 jobid 就像我想计算纽约市的 jobid 我想要像

这样的结果

纽约 3 俄亥俄州 1 弗吉尼亚 2

【问题讨论】:

  • 您自己尝试过吗?这是一个典型的场景。
  • 我按城市名按组绑定它,如果是纽约,它会给出城市名称 3 次并计数 3
  • 在问题中包含您尝试过的查询。
  • 按城市从表中选择城市,计数(*);但这对我没有用@EternalHour

标签: mysql sql string-aggregation


【解决方案1】:
SELECT 
  SUBSTRING_INDEX(SUBSTRING_INDEX(all_city, ',', num), ',', -1) AS one_city,
  COUNT(*) AS cnt
FROM (
  SELECT
    GROUP_CONCAT(city separator ',') AS all_city,
    LENGTH(GROUP_CONCAT(citySEPARATOR ',')) - LENGTH(REPLACE(GROUP_CONCAT(citySEPARATOR ','), ',', '')) + 1 AS count_city
  FROM table_name
) t
JOIN numbers n
ON n.num <= t.count_city
GROUP BY one_city
ORDER BY cnt DESC;

为了获得逗号分隔的不同值的计数,在查询上方运行但获得正确的结果,您应该使用另外一张表**numbers**,它只有一列 num 整数类型并插入一些值。 如果您在 GROUP_CONCAT(city separator ',') AS all_city 在这种情况下遇到错误,请设置一个全局变量“SET group_concat_max_len = 18446744073709547520;”

【讨论】:

【解决方案2】:

试试这个查询,

 SELECT COUNT(jobid), city FROM TABLE where city like '%newyork%';

【讨论】:

    【解决方案3】:

    如下所述设计您的表格。每行中的每个城市名称和职位 ID。

    Job ID      City Name
    1           New York
    2           New York
    1           Seattle
    

    然后使用下面链接中提到的查询。

    SQL: How to get the count of each distinct value in a column?

    【讨论】:

      【解决方案4】:

      您的数据库设计不佳,您将遇到很多麻烦。 使用当前结构,您可以使用 find_in_set 函数获取计数,但您应该避免这样做。

      你的桌子是这样的

      create table test
      (jobid int ,city varchar(100));
      
      insert into test values 
      (1,'New York'),
      (2,'New York, Ohio,Virginia'),
      (3,'New York,Virginia');
      

      现在要获得计数,您可以使用以下方法

      select 
      count(*) as tot from test
      where 
      find_in_set('Virginia',city) > 0;
      

      正如我提到的,这是一个糟糕的数据库设计,理想情况是

      • 首先是一个包含作业详细信息的作业表
      • 包含所有位置的位置表
      • 最后是一个链接工作和位置的表格

      它看起来像

      create table jobs (jobid int, name varchar(100));
      
      insert into jobs values
      (1,'PHP'),(2,'Mysql'),(3,'Oracle');
      
      create table locations (id int, name varchar(100));
      insert into locations values (1,'New York'),(2,'Ohio'),(3,'Virginia');
      
      create table job_locations (id int, jobid int, location_id int);
      
      insert into job_locations values
      (1,1,1),(2,2,1),(3,2,2),(4,2,3),(5,3,1),(6,3,3);
      

      现在获得计数和更多操作将相当容易

      select
      count(j.jobid) as  tot
      from jobs j 
      join job_locations jl on jl.jobid = j.jobid
      join locations l on l.id = jl.location_id
      where
      l.name = 'Virginia'
      

      要计算每个城市的所有工作并使用上述模式,它会非常简单

      select
      l.name,
      count(j.jobid) as  tot
      from jobs j 
      join job_locations jl on jl.jobid = j.jobid
      join locations l on l.id = jl.location_id
      group by l.name
      

      DEMO

      【讨论】:

      • 我想同时计算所有城市的 id,比如纽约 3 弗吉尼亚 2 俄亥俄 1
      【解决方案5】:

      JobIdCity 列设为连接主键。这将使您的生活更轻松。不要插入以逗号分隔的多个城市。

      -------------------------------------------------
      JobId               City         // Other columns
      -------------------------------------------------
      1                   New York
      2                   New York
      2                   Ohio
      2                   Virginia
      3                   New York
      3                   Virginia
      

      现在你的查询将是这样的

      select city, count(jobid) FROM TABLE GROUP BY city  // Result will be New York 3, Ohio 1 and Virginia 2
      

      【讨论】:

      • 根据你的建议,我只能计算一个城市,但我希望一次计算所有城市的工作
      • SELECT COUNT(jobid), city FROM TABLE GROUP BY city
      • 我想要所有城市对应的id计数
      • 你想要结果 4(它只计算一次重复引用)OR 结果 6(它不计算重复城市一次)......那么你的结果是什么想要??
      • 我想要像纽约 3 弗吉尼亚 2 俄亥俄 1 这样的结果
      【解决方案6】:
      SELECT COUNT(*) AS jobs 
        FROM Jobs
       WHERE FIELD_IN_SET('New York') > 0
      ;
      

      不过,您应该阅读有关数据库规范化的信息。在数据库表中具有逗号分隔的值列表总是有“气味”,例如您只能在此处检查特定的城市名称,并且无法一次性轻松地为作业表中引用的所有城市创建作业计数列表...

      参见例如http://en.wikipedia.org/wiki/Database_normalization 开始...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-22
        • 2016-12-03
        • 2020-04-26
        • 2020-08-19
        • 1970-01-01
        • 1970-01-01
        • 2021-09-04
        • 1970-01-01
        相关资源
        最近更新 更多