COUNT的总和(DISTINCT日期)

时间:2016-07-17 23:02:58

标签: mysql sql

我在使用以下SQL时遇到了一些困难:

SELECT SUM(mycount) 
FROM 
    (SELECT COUNT(DISTINCT `User`) AS mycount 
     FROM `DBname` 
     WHERE date(Created) >= '2016-07-01'
    ) as R ; 

目标是在一个月内总结唯一条目的数量。

1 个答案:

答案 0 :(得分:3)

class db {

  protected $db;

  public function __construct() {

      $this->db = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME);

      if ($this->db->connect_errno) {
        die('Error de conexión.');
      }

  }

  public function __destruct() {

    $this->db->close();

  }

 }

class hotelsSearchEngine extends db {

    function autocomplete($term) {

       $request = "SELECT zone_name, destination_name, country_name FROM hotel_destinations WHERE zone_name LIKE '".$term."%'";
       $result = mysqli_query($this->db, $request) or die();

       while($row = $result->fetch_assoc()){

           if ($row['zone_name'] != $row['destination_name']) {

              $terms[] = utf8_encode($row['zone_name']).", ".utf8_encode($row['destination_name']).", ".utf8_encode($row['country_name']);

           }else{

              $terms[] = utf8_encode($row['zone_name']).", ".utf8_encode($row['country_name']);

           };

       }

       echo json_encode($terms);

    }
}