1

我当前的代码显示最近 7 天的投注单,但无论日期如何,我都需要显示最近 7 个投注单。

select 
  betsliphistory.MatchID,Team1.Name as HomeTeam ,
  Team2.Name as AwayTeam, BetSlipID , userid , TipID , 
  matches.ResultTipID ,betsliphistory.`Date`
from betsliphistory
inner join matches on betsliphistory.MatchID = matches.MatchID
inner join teams as Team1 on matches.HomeTeamID = Team1.TeamID
inner join teams as Team2 on matches.AwayTeamID = Team2.TeamID
where userid =".$user." 
  and betsliphistory.`Date` between CURDATE()-7 and CURDATE() 
order by BetSlipID , MatchID;
4

2 回答 2

2

但无论日期如何,我都需要出示最近 7 次投注单。

  • WHERE从子句中删除日期条件。
  • ORDER By BetSlipID DESCwithLIMIT 7用于前 7 名BetSlipID,如下所示:

     ... -- Your current query
     WHERE userid = ".$user." 
     ORDER BetSlipID  DESC
     LIMIT 7;
    

请注意:您的代码以这种方式容易受到SQL Injection的攻击。请改用 PDO 或准备好的语句。有关更多详细信息,请参见:

于 2012-11-29T09:31:47.937 回答
0

试试这个::

 select betsliphistory.MatchID,Team1.Name as HomeTeam , 
                      Team2.Name as AwayTeam, BetSlipID , userid , TipID , matches.ResultTipID ,betsliphistory.`Date`
                      from betsliphistory
                      inner join matches on betsliphistory.MatchID = matches.MatchID
                      inner join teams as Team1 on matches.HomeTeamID = Team1.TeamID
                      inner join teams as Team2 on matches.AwayTeamID = Team2.TeamID
                      inner join (Select DISTINCT(BetSlipID) tempBetSlip from betsliphistory where userid =".$user."  order by `Date` limit 7) temp_betHistory on temp_betHistory.tempBetSlip = BetSlipID
                          where userid =".$user." order by `BetSlipID`
于 2012-11-29T09:32:17.720 回答