简体   繁体   中英

SELECT statement not working in mariadb 10.0 but works in mariadb 5.5

For some reasons, the following select statement does not execute in mariadb 10 but executes well in mariadb 5.5. In 5.5 it picks values from a database in those two time ranges. Fails to pick any on 10. with the same database. What could the problem be? Anyone? Thank you.

$_SESSION['post-data'] = $_POST;

$t1 = $_SESSION['post-data']['t1'];
$t2 = $_SESSION['post-data']['t2'];
$time1 = mysqli_real_escape_string($conn, $t1);
$time2 = mysqli_real_escape_string($conn, $t2);

$sql =  "SELECT DISTINCT msisdn FROM customer WHERE DATE_FORMAT(time_paid,'%Y-%c-%e') 
     BETWEEN ADDDATE('$time1',INTERVAL 0 HOUR) 
        AND ADDDATE('$time2',INTERVAL '23:59' HOUR_MINUTE)";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  echo "Number of Recipients: ";  echo "$result->num_rows <br> <br>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
 $mobilenumber[] = $row['msisdn'];

    }
} else {
    echo "No Contacts to Display";
}

$mob_numbers = implode(", " , $mobilenumber);
echo "$mob_numbers";
$_SESSION['numbers'] = $mob_numbers;

Don't bother using DATE_FORMAT assuming time_paid is DATE or DATETIME or TIMESTAMP . That will let you use an index. Then add this composite INDEX(time_paid, msisdn) ; it will be helpful and "covering".

Then change the BETWEEN to:

SELECT DISTINCT msisdn
    FROM customer
    WHERE time_paid >= $time1
      AND time_paid  < $time2 + INTERVAL 1 DAY

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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