简体   繁体   English

在不同的MySQL列中计数相同的值

[英]Count same values in different column mysql

I need to count how many times in ripeted the same values in different columns for the same id.. I'll try to clarify with an example: TABLE: 我需要计算在同一ID的不同列中的相同值中成熟了多少次。我将尝试通过一个示例进行说明: TABLE:

+-----+-----+-----+-----+-----+
|  id | d01 | d02 | d03 | d04 |
+=====+=====+=====+=====+=====+
|  1  |  A  |  A  | B   |  B  | 
+-----+-----+-----+-----+-----+
|  2  |  A  |  A  |  A  |  A  | 
+-----+-----+-----+-----+-----+
|  3  |  B  |  B  |  A  |  A  | 
+-----+-----+-----+-----+-----+
|  4  |  A  |  A  |  A  |  A  | 
+-----+-----+-----+-----+-----+
|  5  |  A  |  A  |  A  |  A  | 
+-----+-----+-----+-----+-----+
|  6  |  B  |  A  |  A  |  A  | 
+-----+-----+-----+-----+-----+

I need to know how many times the value " B " is repeating for any person (ID) .. 我需要知道“ B ”值对任何人(ID)重复多少次。

Is that possible to do that? 有可能做到这一点吗? RESULTS 结果

+-----+-----+-----+
|  id |  count B  |
+=====+=====+=====+
|  1  |     2     |
+-----+-----+-----+
|  2  |     0     |
+-----+-----+-----+
|  3  |     2     |
+-----+-----+-----+

I was thinking to use the function "SUM" but I have no idea how to display just the single ID. 我当时想使用函数“ SUM”,但我不知道如何仅显示单个ID。 Thanks in advance, hope the question is clear enough! 在此先感谢您,希望问题很清楚!

If there are only four columns: 如果只有四列:

SELECT id, (d01 = 'B') + (d02 = 'B') + (d03 = 'B') + (d04 = 'B')
FROM tablename

No there are 31 columns 没有31列

That's a problem which you can solve in two ways: 您可以通过两种方式解决此问题:

  1. Repeat the condition for the other 27 columns :) 对其他27列重复条件:)
  2. Normalize your structure so that each value is dependent on both the id and a numeric value that represents a calendar. 规范化您的结构,以使每个值都依赖于id和代表日历的数字值。

The PHP way PHP方式

You can also fetch all columns and let PHP solve this for you: 您还可以获取所有列,然后让PHP为您解决:

$res = $db->query('SELECT * FROM tablename');
foreach ($res->fetchAll(PDO::FETCH_ASSOC) as $row) {
    $id = $row['id'];
    unset($row['id']); // don't count the id column
    $count = count(array_keys($row, 'B', true));

    printf("ID %d: %d\n", $id, $count);
}

Since you seem to be using mysql_*: 由于您似乎正在使用mysql_ *:

// SHOW COLUMNS returns all the columns and constrains of the defined table
// We only need the column names so we will be later calling it by 'Field'
$sql = mysql_query("SHOW COLUMNS FROM table"); //your table name here
$val_to_count = 'B'; //value to count here
$id = 1; //id to search for
$new_sql = 'SELECT id, ';

// In this loop we will construct our SELECT query using the columns returned 
// from the above query
while($row=mysql_fetch_array($sql)){
    if($row['Field']!='id'){
            $new_sql .= ' ('.$row['Field'].' = "'.$val_to_count.'") + ';
    }
}

//Removing the last "+ " produced in the select query
$new_sql = rtrim($new_sql,"+ ");
$new_sql .= ' as count FROM table WHERE id = '.$id; //table name here again
// so $new_sql now has an output like: 
// SELECT ID, (d01 = 'B') + (d02 = 'B') ... WHERE id = 1

$sql2 = mysql_query($new_sql);
//executing the constructed query with the output below
while($row2=mysql_fetch_array($sql2)){
 echo 'ID - '.$row2['id']."<br>";
 echo 'Count - '.$row2['count']."<br>";
}

Note: 注意:

mysql_* is deprecated , please consider to migrate to mysqli_* mysql_ *已过时 ,请考虑迁移到mysqli_ *

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

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