如何在where子句中创建2个以上的条件?

时间:2013-06-19 13:21:09

标签: mysql

我在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'的匹配日期我尝试了很多方法来解决它但没有任何作用。 我想知道以这种方式做三个条件是对的吗?它是在两个日期之间获取记录的正确方法吗?

3 个答案:

答案 0 :(得分:2)

根据您的上述评论,您需要将match_date字段的数据类型更改为DATE(YYYY-MM-DD)。 MySQL无法对字符串执行中间操作。为此,您需要在要从字符串转换为日期格式的表列上使用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'

答案 1 :(得分: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')"; 

答案 2 :(得分:-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')
相关问题