简体   繁体   English

如果未使用MySQL IN语句从数组中找到匹配项,则返回0

[英]Return 0 if no match found from array with MySQL IN statement

I know there are many similar questions and answers, but none of them are working in this case, as I'm using IN statement to get matches from array and my dates are in varchar data type because of its format. 我知道有很多类似的问题和答案,但是在这种情况下它们都不起作用,因为我使用IN语句从数组中获取匹配项,并且由于其格式,我的日期为varchar数据类型。 Here it is: 这里是:

I'm trying to check if array items exists in database and get the count for each of them as an array. 我正在尝试检查数据库中是否存在数组项,并获取每个数组的计数。 My SQL query runs fine and gets the results, but the only problem is that I want it to return 0 for array items that does not exist in a database instead of skipping it. 我的SQL查询可以正常运行并获取结果,但是唯一的问题是我希望它为数据库中不存在的数组项返回0,而不是跳过它。

So for example here is my database table: 例如,这是我的数据库表:

postId   reactedTo   reactedDate
126      Like        22 Jun 2019
172      Haha        24 Jun 2019
172      Wow         27 Jun 2019
132      Like        27 Jun 2019

Here is my function to run the SQL Query and get the results as an array 这是我运行SQL查询并以数组形式获取结果的函数

public function reactionsAnalytics() {
    global $wpdb;
    $tableName = $wpdb->prefix.'reactions';
    $dates = $this->getLastNDays(7); //array('22 Jun 2019', '23 Jun 2019', ... ,'28 Jun 2019');
    $reacts = $wpdb->get_results("
SELECT reactedDate, count(*) AS count
  FROM {$tableName} 
 WHERE reactedDate IN ('".implode("','", $dates)."')  
 GROUP 
    BY reactedDate
", ARRAY_A);

    $result = array();
    foreach ($reacts as $react) {
        $result[] = $react['count'];        
    }

    wp_die(json_encode($result));
}

The expected outout of this function is ["1","0","1","0","0","2","0"], but I'm getting ["1","1","2"]. 该函数的预期结果是[“ 1”,“ 0”,“ 1”,“ 0”,“ 0”,“ 2”,“ 0”],但是我得到的是[“ 1”,“ 1” ,“ 2”]。 How can I prevent the $reacts query from skipping not found items and make it output 0 instead? 如何防止$ reacts查询跳过未找到的项目,而是使其输出为0?

I've tried using COALESCE , IFNULL and SUM in various variations but got same results without zeroes. 我尝试在各种变体中使用COALESCEIFNULLSUM ,但得到的结果相同,但没有零。

Here is the SQL Fiddle and you can play with it: 这是SQL Fiddle,您可以使用它:

http://sqlfiddle.com/#!9/ffbb98/5 http://sqlfiddle.com/#!9/ffbb98/5

Thanks! 谢谢!

Instead of trying to complicate the query, you simply need to change your application (PHP) code slightly. 无需尝试使查询复杂化,您只需稍微更改应用程序(PHP)代码即可。 Get the query results for the dates available in the DB. 获取数据库中可用日期的查询结果。 Now, in PHP code, simply check if the count is available for the date or not. 现在,在PHP代码中,只需检查该日期的计数是否可用。 If yes, use the count, else set as zero. 如果是,则使用计数,否则设置为零。

// Change the SQL query result to an array with date as key, and count as value
$reacts_mod = array_combine(array_column($reacts, 'reactedDate'), 
                            array_column($reacts, 'count'));

// Now prepare the $result
$result = array();
// Loop over input dates here
foreach ($dates as $dt) {

    // If you have count obtained from the query result, then consider that else 0
    if ( isset($reacts_mod[$dt]) ) {
        $result[] = $reacts_mod[$dt];
    } else {
        $result[] = 0;
    }

    // Note: PHP7 code would be simply:
    // $result[] = $reacts_mod[$dt] ?? 0;
}

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

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