2

我在 MySQL 数据库比赛、锦标赛和国家/地区中有三个表。我想从这三个表中获取在日期范围内(两天之间)查询的列:

SELECT matches.tournament_id 't_id',
       matches.localteam_ft_score,
       matches.visitorteam_ft_score,
       matches.match_time,
       matches.match_status,
       matches.match_date,
       matches.localteam_id,
       matches.visitorteam_id,
       matches.match_id,
       matches.id,
       matches.static_id,
       matches.localteam_name,
       matches.visitorteam_name,
       matches.halftime_score,
       tournaments.tournament_id,
       tournaments.league_link,
       tournaments.full_league_tr,
       countries.country_name
FROM matches
INNER JOIN tournaments ON tournaments.id = matches.tournament_id
INNER JOIN countries ON tournaments.country_id = countries.country_id
WHERE match_status IN('AET','FT','Pen.','Awarded')
  AND countries.country_name='Worldcup'
  AND matches.match_date BETWEEN '19.05.2013' AND '19.06.2013'

问题是:我无法获得这两个日期之间的记录,它只是给我匹配 '19.05.2013' 日期我尝试了很多方法来解决它,但没有任何效果。我想知道这样做三个条件对不对?这是在两个日期之间获取记录的正确方法吗?

4

3 回答 3

2

根据您上面的评论,您需要将 match_date 字段的数据类型更改为 DATE (YYYY-MM-DD)。MySQL 不能对字符串执行 between 操作。为此,您需要对要从字符串转换为日期格式的表列使用 STR_TO_DATE 函数。

  SELECT matches.tournament_id 't_id', matches.localteam_ft_score,
   matches.visitorteam_ft_score, matches.match_time, matches.match_status,
   matches.match_date, matches.localteam_id, matches.visitorteam_id,
   matches.match_id, matches.id, matches.static_id,matches.localteam_name,
   matches.visitorteam_name, matches.halftime_score,  tournaments.tournament_id,
   tournaments.league_link, tournaments.full_league_tr, countries.country_name 
  FROM matches 
  INNER JOIN tournaments ON tournaments.id = matches.tournament_id 
  INNER JOIN countries ON tournaments.country_id = countries.country_id 
  WHERE match_status IN('AET','FT','Pen.','Awarded') 
  AND countries.country_name='Worldcup' AND STR_TO_DATE(matches.match_date,'%d.%m.%Y') between '2013.05.19' and '2013.06.19'
于 2013-06-19T13:30:10.497 回答
0

检查这个..

query1="date_format(str_to_date(date, '%d/%m/%Y'), '%m/%d/%Y') between date_format(str_to_date('"+fromdate+"', '%d/%m/%Y'), '%m/%d/%Y') and date_format(str_to_date('"+todate+"', '%d/%m/%Y'), '%m/%d/%Y')"; 
于 2013-06-19T13:31:48.033 回答
-1
    SELECT matches.tournament_id 't_id', matches.localteam_ft_score,
    matches.visitorteam_ft_score, matches.match_time, matches.match_status,
    matches.match_date, matches.localteam_id, matches.visitorteam_id,
    matches.match_id, matches.id, matches.static_id,matches.localteam_name,
    matches.visitorteam_name, matches.halftime_score,  tournaments.tournament_id,
    tournaments.league_link, tournaments.full_league_tr, countries.country_name 
    FROM matches 
    INNER JOIN tournaments ON tournaments.id = matches.tournament_id 
    INNER JOIN countries ON tournaments.country_id = countries.country_id 
    WHERE match_status IN('AET','FT','Pen.','Awarded') 
    AND countries.country_name='Worldcup' 
AND (matches.match_date between '19.05.2013' and '19.06.2013' or matches.match_date     between 'd1'and 'd2')
于 2013-06-19T13:35:05.157 回答