简体   繁体   English

MySQL计数出现到新列

[英]mysql count occurrences to new columns

is posible to make a query to count ocurrences in one colum and show the number of ocurrences in different columns? 一个查询来统计一个列中的出现次数并在不同列中显示出现次数的可能性?

Example: 例:

+--------+
| Status |
+--------+
| 2      |
+--------+
| 1      |
+--------+
| 2      |
+--------+
| 3      |
+--------+
| 3      |
+--------+
| 2      |
+--------+

wanted result 想要的结果

+---------+---------+---------+
| status1 | status2 | status3 |
+---------+---------+---------+
| 1       | 3       | 2       |
+---------+---------+---------+

I am limited using VFP ODBC driver 我限于使用VFP ODBC驱动程序

You should really handle display related things in your application code. 您应该真正在应用程序代码中处理与显示相关的事情。 However, you can use Conditional Aggregation with Group By : 但是,您可以将条件聚合与Group By依据一起使用:

SELECT 
  COUNT(CASE status WHEN 1 THEN status END) AS status1, 
  COUNT(CASE status WHEN 2 THEN status END) AS status2, 
  COUNT(CASE status WHEN 3 THEN status END) AS status3
FROM your_table_name 

DB Fiddle DEMO DB小提琴演示

You have tagged the question with VFP, mysql and ODBC so I would assume the question is about doing this in VFP with mySQL backend (since VFP doesn't have an ODBC driver since V6.x). 您已经用VFP,mysql和ODBC标记了该问题,所以我认为问题是关于在带mySQL后端的VFP中执行此操作(因为自V6.x以来,VFP没有ODBC驱动程序)。 And I would also think that your status values might be more than 3, out of order with gaps maybe but less than 256 in total (otherwise if they were constant 1,2,3 you could simply do a count(case when ... then ... end) as status1 ... like query). 而且我还认为您的状态值可能会大于3,顺序不一,缺口可能总共小于256(否则,如果它们恒定为1,2,3,则可以简单地进行计数(当...然后...结束),作为status1 ...之类的查询)。

Then you can get the results from mySQL with a simple query like: 然后,您可以使用以下简单查询从mySQL获取结果:

SQLExec(m.lnYourhandle, 'select status, count(*) as counts'+;
   ' from myTable group by status','crsCounts')

Then cross tab it with a simple code. 然后用一个简单的代码交叉制表。 The whole code would look like: 整个代码如下所示:

Local lnHandle, ix

lnHandle=Sqlstringconnect('Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=myDataBase;'+;
    'User=myUsername;Password=myPassword;Option=3;')
SQLExec(m.lnHandle,'select status, Count(*) as counts'+;
    ' from myTable'+;
    ' group by status'+;
    ' order by status','crsCounts')
SQLDisconnect(0)


Local Array laCounts[1], laStruct[Reccount('crsCounts'),4]
* Get ProductId by transforming it to style
* 'StatusXXX' as field name, i as fieldtype, 4, 0 as field length
Select 'Status'+Padl(Status,3,'0'), 'I', 4, 0 ;
    from crsCounts ;
    into Array laStruct

* Get the counts into an array - already ordered by status
Select counts From crsCounts Into Array laCounts
* transpose the array from being [rows,1] to [1,cols]
Dimension laCounts[1, Alen(laCounts,1)]

* create the result cursor using laStructs array and insert data
Create Cursor crsXTabbed From Array laStruct
Insert Into crsXTabbed From Array laCounts
Browse

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

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