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

时间:2018-04-19 22:39:43

标签: php mysql

我在php中有一个mysql查询,设置为每小时由一个cron作业运行。有时结果是正确的,有时我会得到一个与查询不同的日期的结果。由于mysql正在不同的时区运行,我使用php date() time()来纠正当前的小时和日期。我首先在mysql中使用count来查看是否存在符合条件的任何记录。如果他们这样做我运行相同的查询并获取数据。我正在寻找当天的记录。有一个时间框架,比当前时间早一个小时或一个半小时,并匹配其他几个参数,即JobStatus和TechID列。有时它运行正常,有时我会得到记录显示未来几天。数据库中时间的列是一个varchar,因为我只需要当前时间来匹配字符串,因为约会作为1PM9:30AM或者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"; 或者选择的任何时间段输入到数据库中表格以小时或半小时为准。我也在下面添加了我的数据库结构。我检查了数据库,看看它是否已损坏,并且检查结果正常。 我可能会错误地查询此查询吗?

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;

数据库结构

from __future__ import division
import re
import itertools
import re
import hashlib

def splitFile(lines,splitvalue):
    documents={};
    documentCount=1
    dcmnt="";
    for line in lines:
        dcmnt+=line;
        if (line.__contains__(splitvalue)):
            key="documents"+(str)(documentCount);
            documents[key]=dcmnt;
            dcmnt="";
            documentCount=documentCount+1;
    return documents;

def findSearchSaveMostRepeated(myHash, documentswords):
    tempvalue=0
    for ii in range(0, myHash.__len__()):

        if myHash[documentswords[ii]] > tempvalue :
            tempvalue= myHash[documentswords[ii]]
            x=documentswords[ii]
        if ii + 1 == myHash.__len__():
            #if tempvalue < myHash[documentswords[ii+1]]:
                myHash[x] = 0;
                myLastHash[x] = tempvalue
                return myLastHash[x]





readFile=open("reuters.txt","r");
lines=readFile.readlines();
readFile.close();
alldocuments=splitFile(lines, "</REUTERS>");
#if (alldocuments.count()!=0):
#    print("");
readstopwordFile=open("stopwords.txt","r");
lines2=readstopwordFile.readlines();
readstopwordFile.close();
allstopwords=splitFile(lines2, "\n");
#if (lines.count()!=0):
#    print("");
readFile3=open("reuterdeneme.txt","r");
lines3=readFile3.readlines();
readFile3.close();
alldocumentssecond=splitFile(lines3, "</REUTERS>");

documentswords = []
with open('reuterdeneme.txt','r') as f:  #reading a text file and splitting it into single words
    for line in f:
        for word in line.split():
            documentswords.append(word)
print(documentswords[6]+"example document value")
stopwords=[]
with open('stopwords.txt','r') as f:  #reading a text file and splitting it into single words
    for line in f:
        for word in line.split():
            stopwords.append(word)

print(stopwords[9]+"example stopword value");

#print(stopwords.__len__()) # length of it
numberofwords=documentswords.__len__()
print(numberofwords);

myHash={}
tempofStopwords = []
countTEMP=[]

for i in range(0,documentswords.__len__()):
    count=0;
    for ii in range(0,documentswords.__len__()):


        if documentswords[i]==documentswords[ii]:
            count=count+1;
        if ii+1  == documentswords.__len__():
            print("word")
            print(documentswords[i]);
            print("tanesi");
            print(count);
            tempofStopwords.append(documentswords[i]);
            countTEMP.append(count);
            myHash[documentswords[i]] = count  #words' count



#for i in range(0, myHash.__len__()):
print("hash")
# print(myHash[documentswords[6]]); # I am getting count value with this

print(myHash.keys()); # my key values in myHash


myLastHash={}

tempvalue=0;
print(myHash.__len__())

for i in range(0,100):

    findSearchSaveMostRepeated(myHash,documentswords )  # i am getting the most repeated 100 stopwords


print("my last hash")
print(myLastHash.keys());
print(myLastHash.__len__())
count1=0
count2=0
tempSameStopwords=[]
tempnotSameStopwords=[]
for i in range(0,stopwords.__len__()):
    print(stopwords[i])
    if True==myLastHash.keys().__contains__(stopwords[i]):
        #print("THERE IS")
        count1=count1+1;
        tempSameStopwords.append(stopwords[i])
    if False==myLastHash.keys().__contains__(stopwords[i]):
        #print("THERE IS NOT)
        count2=count2+1;
        tempnotSameStopwords.append(stopwords[i])




print("SAME STOPWORDS")
for i in range(0, tempSameStopwords.__len__()):
    print(tempSameStopwords[i])


print("DIFFERENT STOPWORDS")
for i in range(0, tempnotSameStopwords.__len__()):
    print(tempnotSameStopwords[i])

print("COUNT OF SAME STOPWORDS")
print(count1);
print("COUNT OF DIFFERENT STOP WORDS")
print(count2);



newdocumentswords=[]


print(alldocumentssecond.keys()); #got number of documents -> doc2 from reuterdeneme.txt



for i in range(0,stopwords.__len__()):           #inverted index part.. i am trying to do splitting all documents with single word in every line, and after that i will check which term in which document..
    for ii in range(1,3):
        x = 'file'
        y = ii.__str__();
        x = x + y.__str__();
        z = '.txt'
        m = x + z.__str__();
        #print(m);
        h='documents'.__str__()
        h=h+y;
        #print(h)
        xn = alldocuments[h]
        f = open(m, 'w')
        f.write(xn)
        f.close()
        with open(m, 'r') as f:  # reading a text file and splitting it into single words
            for line in f:
                for word in line.split():
                    newdocumentswords.append(word)

1 个答案:

答案 0 :(得分:1)

在调用AND和OR时,您的查询很混乱,我首先将OR放在括号中

$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";