2017-06-05 45 views
-2

所以我想解決如何檢查所選時間是否空閒。例如我在數據庫中有記錄:檢查日期和時間是否免費

date_from: 2017-06-05 
date_till: 2017-07-05 
Time_showed: 17:00 - 17:05 

時間顯示意味着視頻開始在17:00播放並在17:05結束。 所以現在新的訂單是進入:

date_from: 2017-06-12 
date_till: 2017-09-05 
Time_showed: 17:02 - 17:06 

因此,當我們看到17:02和17:06取(17:03-17:05)之間的時間。任何想法如何檢查與SQL和PHP,所以在這種情況下,它會返回一個錯誤「時間被採取」?

和是啊stackoverflow是不是我來問的第一個地方wjere。在此之前,我試圖谷歌和我自己解決...似乎我不夠強硬來解決它,哈哈。

+1

這是糟糕的桌子設計。你不應該有一個領域。 start_time,end_time列。然後你可以有兩個輸入的表單,檢查開始> =或<=結束等 – clearshot66

+0

你可以用' - '來分割它們嗎? –

+0

你的桌子上有三列嗎?或者有些不同? – Degan

回答

0

好吧,我可以看出你爲什麼需要這樣設計的數據,但它可能值得你花時間來看看它是否可以設計不同。 但是,你來求助,這是我們應該做的,而不是做出判斷。我們都在研究那些沒有以最佳方式設計的數據庫,並且出於任何原因必須與之共存。 (踩下我的肥皂盒..)

所以我想你可能想把它作爲一個存儲過程,你通過日期和顯示時間,並返回一個真正的假。 您需要檢查的條件是: 這是在現有的保留日期和 - 將假定此期間可以爲空 這是在現有的保持時間。 - 會認爲這必須輸入。如果整天將有一個像00:00時 - 24:59

我想出了下面的腳本:

IF EXISTS (SELECT * FROM sys.tables where tables.name like '#myTable%') 
begin 
    drop table #myTable 
end; 


CREATE TABLE #myTable 
(
    date_from date NULL, 
    date_till date NULL, 
    Time_showed varchar(20) 
); 

INSERT INTO #myTable 
VALUES 
    ('2017-06-05', '2017-07-05', '17:00 - 17:05'), 
    ('2017-06-05', '2017-06-05', '08:00 - 08:05'); 

--test data 
DECLARE 
    @test_from date, 
    @test_till as date, 
    @test_time_showed as varchar(20); 
--SELECT 
-- @test_from = '2017-06-12', 
-- @test_till = '2017-07-05', 
-- @test_time_showed = '17:02 - 17:06';--1 
SELECT 
    @test_from = '2017-06-05', 
    @test_till = '2017-06-05', 
    @test_time_showed = '08:00 - 08:00';--0 
--SELECT 
-- @test_from = '2017-06-05', 
-- @test_till = '2017-06-05', 
-- @test_time_showed = '07:59 - 08:00';--0 
--SELECT 
-- @test_from = '2017-06-12', 
-- @test_till = '2017-07-05', 
-- @test_time_showed = '17:07 - 17:16';--0 

--SPLIT 
DECLARE @start_time time = CONVERT(TIME,RTRIM(SUBSTRING(@test_time_showed,0,CHARINDEX('-',@test_time_showed))));--you should turn this into a function (GetStartTime) 
DECLARE @end_time time = CONVERT(TIME,LTRIM(SUBSTRING(@test_time_showed,CHARINDEX('-',@test_time_showed) + 1,len(@test_time_showed))));--you should turn this into a function (GetEndTime) 

----checking values CAN BE DELETED 
--SELECT @start_time,@end_time; 

--SELECT date_from, date_till, 
-- CONVERT(TIME,RTRIM(SUBSTRING(Time_showed,0,CHARINDEX('-',Time_showed)))) AS START_T, 
-- CONVERT(TIME,LTRIM(SUBSTRING(Time_showed,CHARINDEX('-',Time_showed) + 1,len(Time_showed)))) AS END_T 
--FROM #MYTABLE 

--VALIDATION 
--TODO: You need to add some validation to insure that you are getting the correct data. 

--CHECK 
    -- IS 
SELECT CASE 
     WHEN EXISTS (SELECT date_from 
         FROM #myTable 
         WHERE 
          --check if any records on file is for any date in span 
          ((date_from BETWEEN @test_from AND @test_till) OR 
          (date_till BETWEEN @test_from AND @test_till)) AND 
          --if a record is for any date in span, check if times overlap (THE NASTY STUFF) 
          (
           @start_time > CONVERT(TIME,RTRIM(SUBSTRING(Time_showed,0,CHARINDEX('-',Time_showed))))--you should turn this into a function (GetStartTime) 
           AND 
           @start_time < CONVERT(TIME,LTRIM(SUBSTRING(Time_showed,CHARINDEX('-',Time_showed) + 1,len(Time_showed))))--you should turn this into a function (GetEndTime) 
          ) OR 
          (
           @end_time > CONVERT(TIME,RTRIM(SUBSTRING(Time_showed,0,CHARINDEX('-',Time_showed))))--you should turn this into a function (GetStartTime) 
           AND 
           @end_time < CONVERT(TIME,LTRIM(SUBSTRING(Time_showed,CHARINDEX('-',Time_showed) + 1,len(Time_showed))))--you should turn this into a function (GetEndTime) 
          ) 
        ) 
     THEN 1 --TIME PERIOD ALREADY USED 
     ELSE 0 --TIME PERIOD NOT USED 
     END; 

注意:這僅僅是一個開始腳本,也有一些注意事項,我想你需要添加一些額外的功能。 此外,你會想要把它變成一個存儲過程,正如我前面提到的,並創建兩個幫助函數來處理分裂和轉換爲時間,因爲我可以看到你將需要這個其他地方。 我之間沒有使用,因爲如您所述,實際所用時間介於'time_showed'(... 17:02和17:06之間的時間(17:03-17:05)...)之間。 'BETWEEN'將匹配'08:00 - 08:05'和'08:05 - 09:45'。

相關問題