减少这个简单代码的许多SQL查询

时间:2012-12-09 20:37:32

标签: php sql

我有一个数据库表,其中包含事件开始时的日期。当下一个事件开始时,事件结束。给定开始日期和结束日期,我正在寻找创建一个日期数组及其相应的事件ID。

目前,我为每个日期运行SQL查询。伪代码:

$currDate = $startDate
While $currDate++ < $endDate
  echo $currDate . " - " . dbQuery("SELECT TOP 1 ID FROM events WHERE eventDate <= '$currDate' ORDER BY eventDate DESC");
Loop

如何才能提高效率(在PHP中)?

编辑:感谢您的回答,但我意识到这个问题并不清楚。管理编写一些在我的服务器上执行快速(<10ms)的SQL,其中包含跨越多年的日期。

3 个答案:

答案 0 :(得分:2)

select distinct
e1.eventDate,
ID = (
        select top 1 e2.ID 
        from Events e2 
        where e2.eventDate <= e1.eventDate 
        order by e2.eventDate desc
    )
from 
Events e1

当您从查询中获得结果时,您可以将它们循环到代码中并显示结果。提高实施效率的关键是减少数据库往返次数。

答案 1 :(得分:1)

您可以使用类似下面的查询,这样您就可以立即检索整个事件ID列表,日期开始和日期结束:

select id, date_start,(select min(date_start) from eventss where date_start > e.date_start) as date_end from
eventss e where date_start >= @DateStart
and 
(select min(date_start) from eventss 
where date_start > e.date_start) <= @DateEnd

答案 2 :(得分:0)

以下是使用SQL的解决方案:

SQL Fiddle

MS SQL Server 2008架构设置

Create Table dbo.Events (
  ID int not null primary key nonclustered,
  EventDate datetime not null
);


Insert Into dbo.Events (ID, EventDate) Values
  (1, '2011-11-19'),
  (2, '2011-11-25'),
  (3, '2011-12-12'),
  (4, '2012-01-02'),
  (5, '2012-01-05'),
  (6, '2012-02-10');

查询1

DECLARE @dtStart DATETIME,
        @dtEnd   DATETIME

SET @dtStart = '2011-11-01'
SET @dtEnd = '2012-02-20'

SELECT thedate,
       eventID = (SELECT TOP 1 id
                  FROM   events
                  WHERE  eventdate <= thedate
                  ORDER  BY eventdate DESC)
FROM   (SELECT Dateadd(DAY, number, @dtStart) AS theDate --build dates from the start to end date
        FROM   (SELECT DISTINCT number
                FROM   master.dbo.spt_values
                WHERE  name IS NULL) n --returns integer values from 0 to 2037
        WHERE  Dateadd(DAY, number, @dtStart) < @dtEnd) x

<强> Results

|                         THEDATE | EVENTID |
---------------------------------------------
| November, 01 2011 00:00:00+0000 |  (null) |
| November, 02 2011 00:00:00+0000 |  (null) |
| November, 03 2011 00:00:00+0000 |  (null) |
| November, 04 2011 00:00:00+0000 |  (null) |
| November, 05 2011 00:00:00+0000 |  (null) |
| November, 06 2011 00:00:00+0000 |  (null) |
| November, 07 2011 00:00:00+0000 |  (null) |
| November, 08 2011 00:00:00+0000 |  (null) |
| November, 09 2011 00:00:00+0000 |  (null) |
| November, 10 2011 00:00:00+0000 |  (null) |
| November, 11 2011 00:00:00+0000 |  (null) |
| November, 12 2011 00:00:00+0000 |  (null) |
| November, 13 2011 00:00:00+0000 |  (null) |
| November, 14 2011 00:00:00+0000 |  (null) |
| November, 15 2011 00:00:00+0000 |  (null) |
| November, 16 2011 00:00:00+0000 |  (null) |
| November, 17 2011 00:00:00+0000 |  (null) |
| November, 18 2011 00:00:00+0000 |  (null) |
| November, 19 2011 00:00:00+0000 |       1 |
| November, 20 2011 00:00:00+0000 |       1 |
| November, 21 2011 00:00:00+0000 |       1 |
| November, 22 2011 00:00:00+0000 |       1 |
| November, 23 2011 00:00:00+0000 |       1 |
| November, 24 2011 00:00:00+0000 |       1 |
| November, 25 2011 00:00:00+0000 |       2 |
| November, 26 2011 00:00:00+0000 |       2 |
| November, 27 2011 00:00:00+0000 |       2 |
| November, 28 2011 00:00:00+0000 |       2 |
| November, 29 2011 00:00:00+0000 |       2 |
| November, 30 2011 00:00:00+0000 |       2 |
| December, 01 2011 00:00:00+0000 |       2 |
| December, 02 2011 00:00:00+0000 |       2 |
| December, 03 2011 00:00:00+0000 |       2 |
| December, 04 2011 00:00:00+0000 |       2 |
| December, 05 2011 00:00:00+0000 |       2 |
| December, 06 2011 00:00:00+0000 |       2 |
| December, 07 2011 00:00:00+0000 |       2 |
| December, 08 2011 00:00:00+0000 |       2 |
| December, 09 2011 00:00:00+0000 |       2 |
| December, 10 2011 00:00:00+0000 |       2 |
| December, 11 2011 00:00:00+0000 |       2 |
| December, 12 2011 00:00:00+0000 |       3 |
| December, 13 2011 00:00:00+0000 |       3 |
| December, 14 2011 00:00:00+0000 |       3 |
| December, 15 2011 00:00:00+0000 |       3 |
| December, 16 2011 00:00:00+0000 |       3 |
| December, 17 2011 00:00:00+0000 |       3 |
| December, 18 2011 00:00:00+0000 |       3 |
| December, 19 2011 00:00:00+0000 |       3 |
| December, 20 2011 00:00:00+0000 |       3 |
| December, 21 2011 00:00:00+0000 |       3 |
| December, 22 2011 00:00:00+0000 |       3 |
| December, 23 2011 00:00:00+0000 |       3 |
| December, 24 2011 00:00:00+0000 |       3 |
| December, 25 2011 00:00:00+0000 |       3 |
| December, 26 2011 00:00:00+0000 |       3 |
| December, 27 2011 00:00:00+0000 |       3 |
| December, 28 2011 00:00:00+0000 |       3 |
| December, 29 2011 00:00:00+0000 |       3 |
| December, 30 2011 00:00:00+0000 |       3 |
| December, 31 2011 00:00:00+0000 |       3 |
|  January, 01 2012 00:00:00+0000 |       3 |
|  January, 02 2012 00:00:00+0000 |       4 |
|  January, 03 2012 00:00:00+0000 |       4 |
|  January, 04 2012 00:00:00+0000 |       4 |
|  January, 05 2012 00:00:00+0000 |       5 |
|  January, 06 2012 00:00:00+0000 |       5 |
|  January, 07 2012 00:00:00+0000 |       5 |
|  January, 08 2012 00:00:00+0000 |       5 |
|  January, 09 2012 00:00:00+0000 |       5 |
|  January, 10 2012 00:00:00+0000 |       5 |
|  January, 11 2012 00:00:00+0000 |       5 |
|  January, 12 2012 00:00:00+0000 |       5 |
|  January, 13 2012 00:00:00+0000 |       5 |
|  January, 14 2012 00:00:00+0000 |       5 |
|  January, 15 2012 00:00:00+0000 |       5 |
|  January, 16 2012 00:00:00+0000 |       5 |
|  January, 17 2012 00:00:00+0000 |       5 |
|  January, 18 2012 00:00:00+0000 |       5 |
|  January, 19 2012 00:00:00+0000 |       5 |
|  January, 20 2012 00:00:00+0000 |       5 |
|  January, 21 2012 00:00:00+0000 |       5 |
|  January, 22 2012 00:00:00+0000 |       5 |
|  January, 23 2012 00:00:00+0000 |       5 |
|  January, 24 2012 00:00:00+0000 |       5 |
|  January, 25 2012 00:00:00+0000 |       5 |
|  January, 26 2012 00:00:00+0000 |       5 |
|  January, 27 2012 00:00:00+0000 |       5 |
|  January, 28 2012 00:00:00+0000 |       5 |
|  January, 29 2012 00:00:00+0000 |       5 |
|  January, 30 2012 00:00:00+0000 |       5 |
|  January, 31 2012 00:00:00+0000 |       5 |
| February, 01 2012 00:00:00+0000 |       5 |
| February, 02 2012 00:00:00+0000 |       5 |
| February, 03 2012 00:00:00+0000 |       5 |
| February, 04 2012 00:00:00+0000 |       5 |
| February, 05 2012 00:00:00+0000 |       5 |
| February, 06 2012 00:00:00+0000 |       5 |
| February, 07 2012 00:00:00+0000 |       5 |
| February, 08 2012 00:00:00+0000 |       5 |
| February, 09 2012 00:00:00+0000 |       5 |
| February, 10 2012 00:00:00+0000 |       6 |
| February, 11 2012 00:00:00+0000 |       6 |
| February, 12 2012 00:00:00+0000 |       6 |
| February, 13 2012 00:00:00+0000 |       6 |
| February, 14 2012 00:00:00+0000 |       6 |
| February, 15 2012 00:00:00+0000 |       6 |
| February, 16 2012 00:00:00+0000 |       6 |
| February, 17 2012 00:00:00+0000 |       6 |
| February, 18 2012 00:00:00+0000 |       6 |
| February, 19 2012 00:00:00+0000 |       6 |