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 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用