简体   繁体   English

PHP MySQL 计数总问题

[英]PHP MySQL Count Total Issue

I'm almost done with a project and this is my last issue.我几乎完成了一个项目,这是我的最后一个问题。

I have a table where I had to use GROUP_CONCAT like this:我有一张表,我必须像这样使用 GROUP_CONCAT:

SELECT
    GROUP_CONCAT(DISTINCT nextgenorder_companyname) AS nextgenorder_companyname,
    GROUP_CONCAT(DISTINCT nextgenorder_company_entity) AS nextgenorder_company_entity,
    GROUP_CONCAT(DISTINCT nextgenorder_ordernumber) AS nextgenorder_ordernumber,
    GROUP_CONCAT(DISTINCT nextgenorder_deliverydate) AS nextgenorder_deliverydate
FROM nextgenorders2
WHERE nextgenorder_deliverydate='2020-11-11'
GROUP BY SUBSTRING_INDEX(REPLACE(nextgenorder_companyname, '/', ' '), ' ',1)
ORDER BY SUBSTRING_INDEX(REPLACE(nextgenorder_companyname, '/', ' '), ' ',1)

SQL Fiddle Example SQL 小提琴示例

Now I just need to get the total amount of companies on this table:现在我只需要得到这张表上的公司总数: 在此处输入图片说明

For example, the total for the table above would be 3. 3 Total companies.例如,上表的总数为 3。3 家公司总数。

I tried this:我试过这个:

SELECT 
count(DISTINCT nextgenorder_company_entity) as company, 
nextgenorder_deliverydate as ShipDate 
FROM nextgenorders2 
WHERE nextgenorder_deliverydate='2020-11-11' 
    GROUP BY nextgenorder_deliverydate

SQL Fiddle Example SQL 小提琴示例

But the total I get is 6. I just got introduce to GROUP_CONCAT and i know it's needed but I'm getting stuck.但我得到的总数是 6。我刚刚介绍了 GROUP_CONCAT,我知道它是必要的,但我被卡住了。 I also tried this:我也试过这个:

SELECT GROUP_CONCAT(nextgenorder_companyname) AS nextgenorder_companyname,
GROUP_CONCAT(nextgenorder_deliverydate) nextgenorder_deliverydate
  FROM
(
  SELECT COUNT(*) Total
    FROM nextgenorders2
  WHERE nextgenorder_deliverydate='2020-11-11'
 GROUP BY SUBSTRING_INDEX(REPLACE(nextgenorder_companyname, '/', ' '), ' ',1)

) q

You have an expression to extract the "company name" from the column in the database.您有一个表达式可以从数据库的列中提取“公司名称”。

Use that expression for the count(distinct) :将该表达式用于count(distinct)

SELECT count(DISTINCT SUBSTRING_INDEX(REPLACE(nextgenorder_companyname, '/', ' '), ' ',1)
            ) as company, 
       nextgenorder_deliverydate as ShipDate 
FROM nextgenorders2 
WHERE nextgenorder_deliverydate = '2020-11-11' 
GROUP BY nextgenorder_deliverydate;

Here is a db<>fiddle.是一个 db<>fiddle。

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

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