简体   繁体   English

如何编辑此 mySQL 以便将相同 id 的行合并为 1 而不是 3?

[英]How to edit this mySQL so as to combine the rows of the same id into 1 instead of 3?

The following mySQL query gets data from 2 tables, alerts_data and alerts_list .以下 mySQL 查询从 2 个表中获取数据, alerts_dataalerts_list The first table has the data of an alert, and the second has the description of the alert.第一个表包含警报的数据,第二个表包含警报的描述。 So in the alerts_data there are multiple rows with the same alerts_data_id that is the same with the alerts_id of alerts_list .因此,在alerts_data有多个具有相同 alerts_data_id 的行,这些行与 alerts_list 的alerts_list相同。

What i want to achieve, is to display something like this我想要实现的是显示这样的东西

alert number 51, 5 clicked , 2 closed
alert number 57, 13 clicked, 3 closed, 8 waiting

using mySQL or PHP (i do not know if i can get this through plain mySQL)使用 mySQL 或 PHP(我不知道我是否可以通过普通的 mySQL 获得它)

So for now with my knowledge I can not display the data of alert 51 in one row, but because of the different alerts_data_status i have to show 3 rows for each.因此,就我所知,目前我无法在一行中显示警报 51 的数据,但由于不同的alerts_data_status我必须为每个显示 3 行。

How can I do it as above?我该怎么做?

SELECT COUNT( alerts_data_id ) AS total, alerts_data_id, alerts_data_status, alerts_list.alerts_title
FROM  alerts_data 
JOIN alerts_list ON 
alerts_data.alerts_data_id = alerts_list.alerts_id
GROUP BY alerts_data_id, alerts_data_status

//output //输出

total - alerts_data_id - alerts_data_status - alerts_title
5     -     51 - clicked - alert number 51
2     -     52 - closed - alert number 51
13    -     57 - clicked - alert number 57
3     -     57 - waiting - alert number 57
8     -     57 waiting - alert number 57

Note: the alerts number are just examples, it can be any number注意:警报编号只是示例,它可以是任何数字

// alert_data // 警报数据

id - alerts_data_id - alerts_data_status

// alerts_list // 警报列表

alerts_id - alerts_name - alerts_text

Here's a sqlfiddle: http://sqlfiddle.com/#!9/c70c2/1这是一个 sqlfiddle: http ://sqlfiddle.com/#!9/c70c2/1

This may be an application for GROUP_CONCAT() .这可能是GROUP_CONCAT()的应用程序。

You first want a summary of your alerts by alerts_data_id and alerts_data_status.您首先需要通过alerts_data_id 和alerts_data_status 获得警报摘要。 This is a little complex, because your sqlfiddle has a whole bunch of empty alerts_data_status strings.这有点复杂,因为您的 sqlfiddle 有一大堆空的alerts_data_status字符串。 Here, I'm replacing those empty strings with `?'.在这里,我用‘?’替换那些空字符串。 ( http://sqlfiddle.com/#!9/c70c2/23/0 ) ( http://sqlfiddle.com/#!9/c70c2/23/0 )

                      SELECT COUNT(*) AS alerts_count, 
                             alerts_data_id, 
                             CASE WHEN LENGTH(alerts_data_status) = 0 THEN '?'
                                  ELSE alerts_data_status END  AS alerts_data_status
                        FROM alerts_data
                       GROUP BY alerts_data_id, alerts_data_status

You then want to roll that up inside another query然后您想将其汇总到另一个查询中

SELECT SUM(a.alerts_count) total,
       a.alerts_data_id, b. alerts_name,
       GROUP_CONCAT( CONCAT(a.alerts_count, ': ', a.alerts_data_status)
                     ORDER BY a.alerts_data_status
                     SEPARATOR "; " ) detail
  FROM (
                      SELECT COUNT(*) AS alerts_count, 
                             alerts_data_id, 
                             CASE WHEN LENGTH(alerts_data_status) = 0 THEN '?'
                                  ELSE alerts_data_status END  AS alerts_data_status
                        FROM alerts_data
                       GROUP BY alerts_data_id, alerts_data_status
       ) a
  JOIN alerts_list b ON a.alerts_data_id = b.alerts_id
 GROUP BY a.alerts_data_id, b.alerts_name

This will give you one row for each distinct alerts_data_id.这将为每个不同的 alerts_data_id 提供一行。 Each alert is identified by its count, its id, and its name.每个警报由其计数、ID 和名称标识。 ( http://sqlfiddle.com/#!9/c70c2/26/0 ) ( http://sqlfiddle.com/#!9/c70c2/26/0 )

Then the row will contain a semicolon-separated list of the counts of the different alert status.然后该行将包含以分号分隔的不同警报状态的计数列表。

If i understand you well i think here is what you need;如果我理解你,我认为这就是你所需要的;

SELECT 
  COUNT( alerts_data.id ) AS total, 
  ad.id,
  (SELECT count(*) from alert_list al where al.alerts_id=ad.id and alerts_data_status='clicked') as clicked,
  (SELECT count(*) from alert_list al where al.alerts_id=ad.id and alerts_data_status='closed') as closed,
  (SELECT count(*) from alert_list al where al.alerts_id=ad.id and alerts_data_status='waiting') as waiting,
FROM  alerts_data ad
GROUP BY ad.id

Was checking other answers and your SQLFIDDLE and thought this might be a nicer approach:正在检查其他答案和您的 SQLFIDDLE 并认为这可能是一个更好的方法:

SELECT alerts_list.alerts_title,
  SUM(CASE WHEN alerts_data_status = 'clicked' THEN 1 ELSE 0 END) AS clicked,
  SUM(CASE WHEN alerts_data_status = 'closed' THEN 1 ELSE 0 END) AS closed,
  SUM(CASE WHEN alerts_data_status = 'waiting' THEN 1 ELSE 0 END) AS waiting
FROM alerts_data 
JOIN alerts_list ON alerts_data.alerts_data_id = alerts_list.alerts_id
GROUP BY alerts_list.alerts_title

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

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