在 ColdFusion 查询中连接表
Joining Tables in a ColdFusion Query
我正在执行两个查询,以根据同一数据库中两个 table 的数据构建 table。我现在拥有的代码在下面,但我知道我这样做会造成不必要的负担。我一直在尝试加入 tables 以获得相同的结果,但没有成功。有输入吗?
<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
</cfquery>
<cfloop query="GetWeekends">
<cfquery name="GetCommunity">
SELECT community_id, location, language, state, country
FROM _communities
WHERE community_id = #getweekends.community_id#
</cfquery>
<tr>
<td>#DateFormat(start_date, "mm/dd/yyyy")#</td>
<td>#GetComm.location#</td>
<td>#GetComm.state#</td>
<td>#GetComm.country#</td>
<td>#GetComm.language#</td>
</tr>
</cfloop>
数据库连接非常基础。你最好对它们进行自我教育。
无论如何,看起来你想做这样的事情:
<cfquery name="GetWeekends">
SELECT w.id, w.weekend_type, w.community_id, w.start_date, w.end_date,
w.language,
c.community_id, c.location, c.language, c.state, c.country
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
</cfquery>
我正在执行两个查询,以根据同一数据库中两个 table 的数据构建 table。我现在拥有的代码在下面,但我知道我这样做会造成不必要的负担。我一直在尝试加入 tables 以获得相同的结果,但没有成功。有输入吗?
<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
</cfquery>
<cfloop query="GetWeekends">
<cfquery name="GetCommunity">
SELECT community_id, location, language, state, country
FROM _communities
WHERE community_id = #getweekends.community_id#
</cfquery>
<tr>
<td>#DateFormat(start_date, "mm/dd/yyyy")#</td>
<td>#GetComm.location#</td>
<td>#GetComm.state#</td>
<td>#GetComm.country#</td>
<td>#GetComm.language#</td>
</tr>
</cfloop>
数据库连接非常基础。你最好对它们进行自我教育。
无论如何,看起来你想做这样的事情:
<cfquery name="GetWeekends">
SELECT w.id, w.weekend_type, w.community_id, w.start_date, w.end_date,
w.language,
c.community_id, c.location, c.language, c.state, c.country
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
</cfquery>