dongshubang7816 2018-04-19 22:39
浏览 94
已采纳

返回结果中的mySQL AND OR查询不一致

I have a mysql query in php that is set to run hourly by a cron job. Sometimes the results are correct and sometimes I get back a result thats on a different date than being queried. As mysql is being run in a different timezone i am using php date() time() to correct the current hour and date. I first use count in mysql to every see if any records exist matching the criteria. And if they do i run the same query and grab the data. I am looking for records that are for the current day. Have a time frame of an hour or hour and a half ahead of the current time and match a couple other parameters being the JobStatus and TechID columns. Sometimes it runs correctly and sometimes I get records showing that are for days ahead. The column for the time in the database is a varchar as I just simply needed the current time to match a string as the appointment is entered into the database as 1PM or 9:30AM or whatever time slot was selected on the form in hour or half hour time frames. I have as well added my database structure below too. I have checked the database to see if its corrupt and it checks out fine. Am I maybe going about this query incorrectly?

date_default_timezone_set("America/Chicago");

$SearchDate = date('Y-m-d', time());

//CURRENT TIME PLUS ONE HOUR
$TimePlusHour = date('gA', strtotime("+1 hours"));

//CURRENT TIME PLUS ONE HOUR PLUS SETTING MINUTES TO HALF HOUR
$Plus30 = date('g:30A', strtotime("+1 hours"));

$sqlCount = "SELECT COUNT(*) AS 'count' FROM ServiceTickets WHERE Date=
'".$SearchDate."' AND Time='".$Plus30."' OR Time='".$TimePlusHour."' 
AND JobStatus='1' OR JobStatus='3' AND TechID= '".$TechID."' ";

$sql1 = "SELECT * FROM ServiceTickets WHERE Date= '".$SearchDate."' AND
Time='".$Plus30."' OR Time='".$TimePlusHour."' AND JobStatus='1' OR
JobStatus='3' AND TechID= '".$TechID."' ORDER BY id ASC";

Database structure

CREATE TABLE `ServiceTickets` (
`id` bigint(11) unsigned NOT NULL AUTO_INCREMENT,
`FirstName` varchar(255) DEFAULT NULL,
`LastName` varchar(255) DEFAULT NULL,
`Phone` varchar(20) DEFAULT NULL,
`Address` varchar(255) DEFAULT NULL,
`Address2` varchar(255) DEFAULT NULL,
`City` varchar(100) DEFAULT NULL,
`State` varchar(50) DEFAULT NULL,
`Zip` varchar(10) DEFAULT NULL,
`Email` varchar(255) DEFAULT NULL,
`Date` date DEFAULT NULL,
`Time` varchar(50) DEFAULT NULL,
`JobDesc` text,
`JobStatus` varchar(10) DEFAULT NULL,
`TechID` varchar(3) DEFAULT NULL,
`LastModified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) 
ENGINE=MyISAM AUTO_INCREMENT=21 DEFAULT CHARSET=latin1;
  • 写回答

1条回答 默认 最新

  • duanliang8464 2018-04-20 03:14
    关注

    You are confusing your query when calling AND and OR, I would start by putting your OR in parentheses

    $sqlCount = "SELECT COUNT(*) AS 'count' FROM ServiceTickets WHERE Date=
    '".$SearchDate."' AND (Time='".$Plus30."' OR Time='".$TimePlusHour."') 
    AND (JobStatus='1' OR JobStatus='3') AND TechID= '".$TechID."' ";
    
    $sql1 = "SELECT * FROM ServiceTickets WHERE Date= '".$SearchDate."' AND
    (Time='".$Plus30."' OR Time='".$TimePlusHour."') AND (JobStatus='1' OR
    JobStatus='3') AND TechID= '".$TechID."' ORDER BY id ASC";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请提供一个符合要求的网页链接。
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码