mck mck - 1 year ago 102
MySQL Question

Joining Tables in a ColdFusion Query

I am executing two queries to build a table off of data from two tables in the same database. The code I have now is below, but I know I am creating an unnecessary load doing it this way. I have been trying to join the tables to get the same result, but have had no luck. Any input?

<cfquery name="GetWeekends">
SELECT id, weekend_type, community_id, start_date, end_date, language
FROM _weekends
WHERE weekend_type = 1 and start_date > Now()
ORDER BY start_date ASC

<cfloop query="GetWeekends">
<cfquery name="GetCommunity">
SELECT community_id, location, language, state, country
FROM _communities
WHERE community_id = #getweekends.community_id#
<td>#DateFormat(start_date, "mm/dd/yyyy")#</td>

Answer Source

Database joins are pretty basic. You'd do well to educate yourself about them.

In any event, it looks like you want to do something like this:

<cfquery name="GetWeekends">
SELECT, w.weekend_type, w.community_id, w.start_date, w.end_date,
  c.community_id, c.location, c.language, c.state,
FROM _weekends w
  INNER JOIN _communities c
    ON w.community_id=c.community_id
WHERE w.weekend_type = 1 and w.start_date > Now()
ORDER BY w.start_date ASC
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download