根据行的值比较列

时间:2013-11-15 01:52:25

标签: php mysql multiple-columns rows

我是MySQL语法的新手,请帮忙。

更新了查询:

$query = mysql_query("SELECT  campaign, team, employeeName, shiftcode, scheduleDate,STR_TO_DATE(scheduleIn,'%h:%i %p')  as ScheduleIn, STR_TO_DATE(scheduleOut,'%h:%i %p')  as ScheduleOut, STR_TO_DATE(MID(scheduleIn,7,4), '%h:%i') as sIn FROM tbl_uploadedschedule WHERE NOT EXISTS(SELECT scOrigtimein, scOrigtimeout FROM tbl_shiftcode WHERE STR_TO_DATE(tbl_uploadedschedule.scheduleIn,'%h:%i %p') = tbl_shiftcode.scOrigtimein or STR_TO_DATE(tbl_uploadedschedule.scheduleOut,'%h:%i %p')=tbl_shiftcode.scOrigtimeOut )") or die(mysql_error());

但它什么都不返回,不知道STR_TO_DATE()是否与WHERE子句无法正常工作

1 个答案:

答案 0 :(得分:0)

尝试使用NOT EXISTS之类的:

SELECT * FROM TableB
WHERE NOT EXISTS 
(SELECT * FROM TableA
 WHERE TableB.Shift_Codes=TableA.Shift_Codes
 AND TableB.Time_in = TableA.Time_in 
 AND TableB.Time_out = TableA.Time_out)

请参阅Fiddle Demo

使用STR_TO_DATE()

SELECT * FROM TableB
WHERE NOT EXISTS 
(SELECT * FROM TableA
 WHERE TableB.Shift_Codes=TableA.Shift_Codes
 AND STR_TO_DATE(TableB.Time_in,'%h:%i:%s') = STR_TO_DATE(TableA.Time_in,'%h:%i:%s') 
 AND STR_TO_DATE(TableB.Time_out,'%h:%i:%s') = STR_TO_DATE(TableA.Time_out,'%h:%i:%s'))

请参阅Demo