我有一个查询,返回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”与我的查询结果“ equip”在where子句中的第一条记录进行比较,即使返回了七个记录。
有办法解决吗?
最佳答案
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>