SQL查询DATE_FORMAT函数无法正常工作

时间:2016-01-04 15:31:23

标签: php mysql

给定的mysql查询没有返回任何数据

SELECT DISTINCT users.* FROM type_b_services JOIN users ON
 users.userid=type_b_services.userid WHERE type_b_services.offer = 'A'
 AND type_b_services.where_you_based = 'Domestic' AND
 type_b_services.origin_country = 'United States' AND
 type_b_services.origin_city = 'seattle' AND
 type_b_services.destination_country = 'United States' AND
 type_b_services.destination_city = 'San Francisco' AND
 TRIM(DATE_FORMAT(type_b_services.travel_date,"%Y-%m")) =
 TRIM(DATE_FORMAT('2016-02-22',"%Y-%m"))

但是这一行返回1行

SELECT DISTINCT users.* FROM type_b_services JOIN users ON
 users.userid=type_b_services.userid WHERE type_b_services.offer = 'A'
 AND type_b_services.where_you_based = 'Domestic' AND
 type_b_services.origin_country = 'United States' AND
 type_b_services.origin_city = 'seattle' AND
 type_b_services.destination_country = 'United States' AND
 type_b_services.destination_city` = 'San Francisco' 

我发现这种情况打破了数据。

  

TRIM(DATE_FORMAT(type_b_services.travel_date,“%Y-%m”))=   TRIM(DATE_FORMAT( '2016年2月22日', “%Y-%M”))

但是下面的代码返回18列,包括第二次查询的结果

SELECT id, DATE_FORMAT(type_b_services.travel_date, "%Y-%m") ,  
   DATE_FORMAT("2016-02-06", "%Y-%m") FROM `type_b_services` 
WHERE   
    TRIM(DATE_FORMAT(type_b_services.travel_date,"%Y-%m")) = 
    TRIM(DATE_FORMAT('2016-02-22',"%Y-%m"))

0 个答案:

没有答案