我有一个经典ASP页面,可让用户搜索属性和结果页面,然后通过电子邮件发送具有匹配属性的属性代理,让他们知道有租户对其属性感兴趣。
某些代理商可能拥有1个以上的房产,在这种情况下,代理商只需要收到1封电子邮件,而不是每封房产的电子邮件。目前我的代码正在执行后者,我需要它只向每个代理发送一封电子邮件。
在下面的代码中,我有2个记录集; rspropertyresults显示所有匹配的属性,然后我有rsemailagents用于收集代理的电子邮件地址并向他们发送电子邮件。
我想知道是否有人能够看到我如何修改rsemailagents的sql查询以便为每个代理发送1封电子邮件?
<%
[...]
rsemailagents.Source = "SELECT *"
rsemailagents.Source = rsemailagents.Source& "FROM ("
rsemailagents.Source = rsemailagents.Source& " SELECT ContentID"
rsemailagents.Source = rsemailagents.Source& " FROM ("
rsemailagents.Source = rsemailagents.Source& " SELECT ContentID"
rsemailagents.Source = rsemailagents.Source& " FROM VWTenantPropertiesResults"
rsemailagents.Source = rsemailagents.Source& " WHERE ContentStreet = '" & Replace(rsemailagents__varReqStreet, "'", "''") & "'"
rsemailagents.Source = rsemailagents.Source& " UNION ALL"
rsemailagents.Source = rsemailagents.Source& " SELECT ContentID"
rsemailagents.Source = rsemailagents.Source& " FROM VWTenantPropertiesResults"
rsemailagents.Source = rsemailagents.Source& " WHERE ContentTown = '" & Replace(rsemailagents__varReqTown, "'", "''") & "'"
rsemailagents.Source = rsemailagents.Source& " UNION ALL"
rsemailagents.Source = rsemailagents.Source& " SELECT ContentID"
rsemailagents.Source = rsemailagents.Source& " FROM VWTenantPropertiesResults"
rsemailagents.Source = rsemailagents.Source& " WHERE ContentTrimmedPostCode LIKE '" & Replace(varPostcode, "'", "''") & "%'"
rsemailagents.Source = rsemailagents.Source& " ) qi"
rsemailagents.Source = rsemailagents.Source& " GROUP BY"
rsemailagents.Source = rsemailagents.Source& " ContentID"
rsemailagents.Source = rsemailagents.Source& " HAVING COUNT(*) >= 2"
rsemailagents.Source = rsemailagents.Source& " ) q "
rsemailagents.Source = rsemailagents.Source& "JOIN VWTenantPropertiesResults r "
rsemailagents.Source = rsemailagents.Source& "ON r.ContentID = q.ContentID "
rsemailagents.Source = rsemailagents.Source& "WHERE ContentBedrooms BETWEEN " & Replace(rsemailagents__varBedroomsNoMin, "'", "''") & " "
rsemailagents.Source = rsemailagents.Source& "AND " & Replace(rsemailagents__varBedroomsNoMax, "'", "''") & " "
rsemailagents.Source = rsemailagents.Source& "AND ContentPrice BETWEEN " & Replace(rsemailagents__varPriceMin, "'", "''") & " "
rsemailagents.Source = rsemailagents.Source& "AND " & Replace(rsemailagents__varPriceMax, "'", "''") & " " & varSQL & " "
rsemailagents.Source = rsemailagents.Source& "ORDER BY"
rsemailagents.Source = rsemailagents.Source& " ContentPrice " & Replace(rsemailagents__varSortWay, "'", "''") & " "
rsemailagents.Source = rsemailagents.Source& "GROUP BY CustomerEmail"
[...]
%>
在我的电子邮件代码中,我将objMail.To设置如下;
objMail.To = (rsemailagents.Fields.Item("CustomerEmail").Value)
这是VWTenantPropertiesResults代码;
SELECT dbo.VWResidentialLettings.ContentID, dbo.VWResidentialLettings.ContentTitle, dbo.VWResidentialLettings.ContentBriefText,
dbo.VWResidentialLettings.ContentDetails, dbo.VWResidentialLettings.ContentHouseNo, dbo.VWResidentialLettings.ContentStreet,
dbo.VWResidentialLettings.ContentStreet2, dbo.VWResidentialLettings.ContentTown, dbo.VWResidentialLettings.ContentArea,
dbo.VWResidentialLettings.ContentCounty, dbo.VWResidentialLettings.ContentPostCode, dbo.VWResidentialLettings.ContentReference,
dbo.VWResidentialLettings.ContentPrice, dbo.VWResidentialLettings.ContentPricePeriod, dbo.VWResidentialLettings.ContentPriceText,
dbo.VWResidentialLettings.ContentPropertyType, dbo.VWResidentialLettings.PropertyTypeTitle, dbo.VWResidentialLettings.ContentPropertyListType,
dbo.VWResidentialLettings.PropertyListTypeTitle, dbo.VWResidentialLettings.PricePeriodTitle, dbo.VWResidentialLettings.BedRoomNoTitle,
dbo.VWResidentialLettings.ContentBedRooms, dbo.VWResidentialLettings.ContentFurnishing, dbo.VWResidentialLettings.FurnishTypeTitle,
dbo.VWPropertyAgentsActiveSubscriptions.CustomerID, dbo.VWPropertyAgentsActiveSubscriptions.CustomerName,
dbo.VWPropertyAgentsActiveSubscriptions.CustomerBusiness, dbo.VWPropertyAgentsActiveSubscriptions.CustomerAddress1,
dbo.VWPropertyAgentsActiveSubscriptions.CustomerAddress2, dbo.VWPropertyAgentsActiveSubscriptions.CustomerCity,
dbo.VWPropertyAgentsActiveSubscriptions.CustomerArea, dbo.VWPropertyAgentsActiveSubscriptions.CustomerRegion,
dbo.VWPropertyAgentsActiveSubscriptions.CustomerPostalCode, dbo.VWPropertyAgentsActiveSubscriptions.CustomerPhone,
dbo.VWPropertyAgentsActiveSubscriptions.CustomerFax, dbo.VWPropertyAgentsActiveSubscriptions.CustomerURL,
dbo.VWPropertyAgentsActiveSubscriptions.CustomerEmail, dbo.VWResidentialLettings.CountyTitle, dbo.VWResidentialLettings.ContentCreated,
dbo.VWResidentialLettings.ContentUpdated, dbo.VWResidentialLettings.ContentStatus, dbo.VWPropertyImagesList.DocumentFile,
dbo.VWPropertyImagesList.DocumentTitle, dbo.VWResidentialLettings.ContentTrimmedPostCode, dbo.VWPropertyImagesList.ContentStatusTitle
FROM dbo.VWResidentialLettings INNER JOIN
dbo.VWPropertyAgentsActiveSubscriptions ON
dbo.VWResidentialLettings.ContentParentID = dbo.VWPropertyAgentsActiveSubscriptions.CustomerID LEFT OUTER JOIN
dbo.VWPropertyImagesList ON dbo.VWResidentialLettings.ContentID = dbo.VWPropertyImagesList.ContentID
WHERE (dbo.VWResidentialLettings.ContentStatus = 2)
当我在数据库中运行此查询时,它只返回一个CustomerEmail列而不是其他内容;
SELECT DISTINCT CustomerEmail
FROM (
SELECT ContentID
FROM (
SELECT ContentID
FROM VWTenantPropertiesResults
WHERE ContentStreet = 'Hull'
UNION ALL
SELECT ContentID
FROM VWTenantPropertiesResults
WHERE ContentTown = ''
UNION ALL
SELECT ContentID
FROM VWTenantPropertiesResults
WHERE ContentTrimmedPostCode LIKE 'HU7'
) qi
GROUP BY
ContentID
HAVING COUNT(*) >= 2
) q
JOIN VWTenantPropertiesResults r
ON r.ContentID = q.ContentID
WHERE ContentBedrooms BETWEEN 1 AND 10 AND ContentPrice BETWEEN 1 AND 10
答案 0 :(得分:2)
将您的查询放入此查询的IN
子句
SELECT email
FROM agents
WHERE id IN
(
SELECT agentID
FROM …
)
这将仅选择每个代理一次。
答案 1 :(得分:0)
好的...
尝试创建一个子查询,将所有属性组合在一行中。
您可以通过查询创建一个组来完成此操作:
select
contentID , count(*) hits
from
VWTenantPropertiesResults
where
propertyValue in ('A','B','C')
group by
contentID
此外,您很容易受到SQL注入攻击,因为您在没有正确转义SQL的情况下创建了SQL。在这里考虑参数化查询或存储过程。
您还有一种非常复杂的创建查询的方法。您可以在10多个步骤中构建一个字符串,并将其分配给该属性。考虑在这里使用stringbuilder,因为字符串是不可变的,这可能会导致性能问题(特别是在循环中使用这样的东西时...如果你需要超过5个concats,那么考虑使用stringbuilder)
答案 2 :(得分:0)
您有别名'q'的子查询有一个字段ContentID。除非你有非常奇怪的命名约定,否则这不是电子邮件地址。因此,您告诉它“选择CustomerEmail From(没有名为CustomerEmail的字段的类似表的对象)”。这不可能奏效,我很惊讶它返回的不是错误消息。
尝试查看“IN”子句或“EXISTS”子句。