CFQuery where子句与查询结果的比较
CFQuery where clause comparison to query results
我有一个查询 returns 7 条记录:
<cfquery name="equip" datasource="#datasource#">
select equipment_id from service_ticket
where equipment_id > 0
</cfquery>
然后我获取这些结果并再次使用 运行 另一个查询:
<cfquery name="get_equipment_tally" datasource="#datasource#">
select *
from equipment_maintenance
where active = "yes" AND id <> #equip.equipment_id#
</cfquery>
但是查询 "get_equipment_tally" 仅将 'id' 与 where 子句中我的查询结果 "equip" 中的第一条记录进行比较,即使返回了七条记录。
有解决办法吗?
AND id NOT IN <cfqueryparam cfsqltype="cf_sql_integer"
value="#valueList(equip.equipment_id)#" list="true">
理想情况下,您会把它变成一个查询
<cfquery name="get_equipment_tally" datasource="#datasource#">
select *
from equipment_maintenance
where active = "yes" AND id NOT IN (select equipment_id from service_ticket where equipment_id > 0)
</cfquery>
我喜欢这些答案,但在查询中看到 NOT IN 让我感到不安。使用 LEFT OUTER JOIN... IS NULL 代替。会快很多。
SELECT <list of cols here>
FROM equipment_maintenance em
LEFT OUTER JOIN equipment_id ei ON ei.equipment_id = em.equipment_id
WHERE em.Active = 'yes'
AND ei.equipment_id IS NULL
类似的东西。
我有一个查询 returns 7 条记录:
<cfquery name="equip" datasource="#datasource#">
select equipment_id from service_ticket
where equipment_id > 0
</cfquery>
然后我获取这些结果并再次使用 运行 另一个查询:
<cfquery name="get_equipment_tally" datasource="#datasource#">
select *
from equipment_maintenance
where active = "yes" AND id <> #equip.equipment_id#
</cfquery>
但是查询 "get_equipment_tally" 仅将 'id' 与 where 子句中我的查询结果 "equip" 中的第一条记录进行比较,即使返回了七条记录。
有解决办法吗?
AND id NOT IN <cfqueryparam cfsqltype="cf_sql_integer"
value="#valueList(equip.equipment_id)#" list="true">
理想情况下,您会把它变成一个查询
<cfquery name="get_equipment_tally" datasource="#datasource#">
select *
from equipment_maintenance
where active = "yes" AND id NOT IN (select equipment_id from service_ticket where equipment_id > 0)
</cfquery>
我喜欢这些答案,但在查询中看到 NOT IN 让我感到不安。使用 LEFT OUTER JOIN... IS NULL 代替。会快很多。
SELECT <list of cols here>
FROM equipment_maintenance em
LEFT OUTER JOIN equipment_id ei ON ei.equipment_id = em.equipment_id
WHERE em.Active = 'yes'
AND ei.equipment_id IS NULL
类似的东西。