问题
I have an events page where I need to display the events for each day. I've gotten it to this point, so I'm making progress.
The database has 3 tables: fairdays, eventtypes, events
- fairdays: id, fairdaydate (datetime), daycolor, description 
- eventtypes: id, eventtype <-- This table is for the input select in the "add event form" 
- events: id, eventname, eventtype, eventday (datetime), eventtime (datetime) 
My intent is to display the day with the description, then under that is the event types, then under each of those is the corresponding events.
I haven't worked out how to display the event type sub heading, then the individual events under each of those, but here's my code so far. Any help would be hugely appreciated.
<cfquery datasource="fairscheduledb" name="getfairdays">
    select * from fairdays
</cfquery>
<cfquery datasource="fairscheduledb" name="getfairevents">
    select * from events ev
    inner join fairdays fd on fd.fairdaydate = ev.eventday
    where ev.eventday = fd.fairdaydate
</cfquery>
<cfloop query="getfairdays">
<cfoutput>
    <div class="schedulebox">
        <div class="schedulehead" style="color: ###getfairdays.daycolor#;">#dateformat(getfairdays.fairdaydate,"dddd, mmmm dd")#</div>
        <div class="schedulesubhead" style="color: ##ffffff; background: ###getfairdays.daycolor#;">#getfairdays.description#</div>
        <cfoutput query="getfairevents">
            <div class="scheduleitem" style="float: left; width: 75px; text-align: right;">#LCase(TimeFormat(getfairevents.eventtime,"h:mmtt"))#</div>
            <div class="scheduleitem" style="float: left; width: 550px;">#getfairevents.eventname#</div><br/>
        </cfoutput>
    </div>
</cfoutput>
</cfloop>
Here's what the list of days and events should look like:
<div class="schedulebox">
    <div class="schedulehead" style="color: #4CC417;">Friday, February 22</div>
    <div class="schedulesubhead" style="color: #ffffff; background: #4CC417;">Opening Ceremony 4:30pm at Gate<br/>5:00 - 6:00pm - Free Admission & Free Rides</div>
    <div class="scheduleitemtitle" style="color: #4CC417;">Strolling Acts</div>
        <div class="scheduleitem">5:30pm - Scotts Magic Show</div>
        <div class="scheduleitem">6:30pm - Rock-It the Robot</div>
        <div class="scheduleitem">7:30pm - Scotts Magic Show</div>
        <div class="scheduleitem">8:30pm - Rock-It the Robot</div>
    <div class="scheduleitemtitle" style="color: #4CC417;">Acts</div>
        <div class="scheduleitem">5:30pm - Sea Lion Show</div>
        <div class="scheduleitem">6:00pm - Alligator Wrestling</div>
        <div class="scheduleitem">6:30pm - Petting Zoo Presentation </div>
        <div class="scheduleitem">8:00pm - Alligator Wrestling</div>
        <div class="scheduleitem">8:30pm - Petting Zoo Presentation </div>
        <div class="scheduleitem">9:00pm - Sea Lion Show</div>
        <div class="scheduleitemtitle" style="color: #4CC417;">Stage Acts</div>
        <div class="scheduleitem">7:00pm - Youth Royalty</div>
    <div class="scheduleitemtitle" style="color: #4CC417;">Livestock Program</div>
        <div class="scheduleitem">6:00pm - Beef Breeding Screening</div>
        <div class="scheduleitem">7:00pm - Horse Judging Competition</div>
</div>
回答1:
(This is too long for comments ...)
To expand on Dan's answer, he is suggesting a more efficient way of producing that output by using JOIN's and cfoutput's group feature (emphasis is mine):
... Eliminates adjacent duplicate rows when data is sorted. Use if you retrieved a record set ordered on one or more a query columns. For example, if a record set is ordered on "Customer_ID" in the cfquery tag, you can group the output on "Customer_ID."
For your JOIN's you will need to include all three tables to grab all of the columns you need. I cannot test it right now, but something along these lines. (Notice the results are sorted the same way you wish to display them ie by event date, type and time)
SELECT  ev.eventDay, t.EventType, ev.EventTime
FROM    fairdays fd 
          INNER JOIN events ev ON ev.eventDay = fd.fairdaydate
          INNER JOIN eventType t ON t.ID = ev.EventType
ORDER BY ev.eventDay, t.EventType, e.EventTime
Once you have the sorted results, use "group" generate the desired results. Be sure to group by the same columns, in the same order, as the sql query. Otherwise, it will not work correctly. 
<cfoutput query="yourQuery" group="EventDay">    
    <!--- display event dates --->     
     #EventDay# <hr/>    
    <!--- event types for current date --->
    <cfoutput group="EventType">
        #EventType#<br/>
        <!--- individual events --->
        <cfoutput>
            #EventTime# <br/>
        </cfoutput>
    </cfoutput> 
</cfoutput>
Update from comments:
As discussed in the comments, if you want to retrieve all fairdays (even ones without a matching event) use outer joins instead of inner joins.
SELECT  fd.fairDayDate, t.ID, ev.EventType, ev.EventTime
FROM    fairdays fd 
          INNER JOIN events ev ON ev.eventDay = fd.fairDayDate
          INNER JOIN eventType t ON t.ID = ev.EventType
ORDER BY fd.fairDayDate, t.ID, e.EventTime
Again, since the cfoutput "group" feature requires sorted query data to work properly, if you change the ORDER BY clause, be sure to update the "group" columns to match your ORDER BY clause. ie Group by fairDayDate first, then event ID:
<cfoutput query="yourQuery" group="fairDayDate">    
    <!--- display event dates --->     
     #fairDayDate# <hr/>    
    <!--- event types for current date --->
    <cfoutput group="ID">
        #EventType#<br/>
        <!--- individual events --->
        <cfoutput>
            #EventTime# <br/>
        </cfoutput>
    </cfoutput> 
</cfoutput>
回答2:
First, you don't need the first query.
Next, add
order by eventday
to query getfairevents. That will enable you to do this:
<cfoutput query="getfairevents" group = "eventday">
    #eventday#
    <cfoutput>
        output other stuff here (individual events and times)
    </cfoutput>
</cfoutput>
来源:https://stackoverflow.com/questions/21483485/how-can-i-display-each-days-events