且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将PHP中的两个值与mysql数据库中的两列进行比较?

更新时间:2023-01-21 12:24:21

如果我理解正确的话,试试这个:

If I understand you correctly, try this:
SELECT * FROM books WHERE (start_hour >= '6:00' OR end_hour <= '12:00') AND book_id=2



你需要一个'OR'作为start_hour,end_hour可以跨越多个行,并使用book_id条件在'AND'之前的括号中设置'OR'条件。



+++++ [round 2] +++++

将字符串参数转换为时间,然后使用 STR_TO_DATE()函数 [ ^ ]


You need an 'OR' as the start_hour and end_hour can span more than one row, and set the 'OR' condition in brackets before 'AND' with the book_id condition.

+++++[round 2]+++++
Convert the string parameters to time, then compare using STR_TO_DATE() function[^]

SELECT * FROM books WHERE (STR_TO_DATE(start_hour, '%h:%m') >= STR_TO_DATE('13:00', '%h:%m')
OR STR_TO_DATE(end_hour, '%h:%m') <= STR_TO_DATE('22:00', '%h:%m')) 
AND book_id=2



希望这次有用。

最后但并非最不重要的是,你应该认真考虑使用最适合其预期目的的数据类型,即 MySQL :: MySQL 5.7参考手册:: 11。3日期和时间类型 [ ^ ]


说实话,我不太明白为什么你只处理时间而不考虑日期。但无论如何,我建议使用时间或日期时间数据类型。这使得计算比使用字符串更容易。



例如,对于time数据类型,您的声明和查询可能类似于以下内容。我们的想法是查询搜索预留,因此如果它返回一行或几行,则该书不可用。如果它没有返回任何行,则该书可用。



请考虑以下示例:

To be honest I don't quite understand why you handle only times and don't take dates into account. But regardless of that, I would suggest using either time or datetime data type. This makes calculation a lot easier than playing with strings.

For example with time datatype your declarations and queries could look something like the following. The idea is that the query searches for reservations so if it returns a row or several rows, the book isn't available. If it returns no rows, the book is available.

Consider the following examples:
CREATE TABLE Books2 (
   id        int, 
   book_id   int, 
   starttime time, 
   endtime   time)
;
	
INSERT INTO Books2 (id, book_id, starttime, endtime)
VALUES
	(1, 2, convert(time, '1900-01-01 06:00:00.000', 121), convert(time, '1900-01-01 10:00:00.000', 121)),
	(2, 2, convert(time, '1900-01-01 11:00:00.000', 121), convert(time, '1900-01-01 12:00:00.000', 121)),
	(3, 1, convert(time, '1900-01-01 13:00:00.000', 121), convert(time, '1900-01-01 19:00:00.000', 121)),
	(4, 1, convert(time, '1900-01-01 20:00:00.000', 121), convert(time, '1900-01-01 22:00:00.000', 121))
;


-- test 1, fail
DECLARE @teststart varchar(100) = '1900-01-01 07:00:00.000';
DECLARE @testend varchar(100) = '1900-01-01 11:00:00.000';

-- test 2, fail
--DECLARE @teststart varchar(100) = '1900-01-01 07:00:00.000';
--DECLARE @testend varchar(100) = '1900-01-01 17:00:00.000';

-- test 3, fail
--DECLARE @teststart varchar(100) = '1900-01-01 07:00:00.000';
--DECLARE @testend varchar(100) = '1900-01-01 10:00:00.000';

-- test 4, fail
--DECLARE @teststart varchar(100) = '1900-01-01 05:00:00.000';
--DECLARE @testend varchar(100) = '1900-01-01 17:00:00.000';


-- test 5, succeed
--DECLARE @teststart varchar(100) = '1900-01-01 05:00:00.000';
--DECLARE @testend varchar(100) = '1900-01-01 06:00:00.000';

-- test 6, succeed
--DECLARE @teststart varchar(100) = '1900-01-01 14:00:00.000';
--DECLARE @testend varchar(100) = '1900-01-01 17:00:00.000';

SELECT * -- row found, book not available, row not found, book available
FROM Books2 
WHERE book_id = 2
AND (	
       (	convert(time, @teststart, 121) <= starttime
		AND convert(time, @testend, 121) > starttime) -- test time ends within non-allowed time
	OR (	convert(time, @teststart, 121) < endtime
		AND convert(time, @testend, 121) >= endtime) -- test time starts within non-allowed time
	OR (	convert(time, @teststart, 121) < endtime
		AND convert(time, @testend, 121) >= endtime)
    OR  (	( convert(time, @teststart, 121) > starttime and convert(time, @teststart, 121) < endtime )
		OR	( convert(time, @testend, 121) > starttime and convert(time, @testend, 121) < endtime ) ) -- test time is within non-allowed time
	);