简体   繁体   English

如何在SQL查询中使用

[英]How to use exists on SQL Query

I have a small question on this piece of code: 我对这段代码有一个小问题:

WITH cps AS 
(
    SELECT 
        cp.id_campaign, ean.equipment 
    FROM 
        tbl_campaign cp
    INNER JOIN 
        tbl_equip ean ON (cp.id_campaign = ean.id_campaign)
)       
SELECT 
    COUNT(cp.id_campaign)
FROM 
    cps cp 
INNER JOIN 
    tbl_camp_associates assoc ON (cp.id_campaign = assoc.id_campaign_associate)
WHERE 
    EXISTS (SELECT * FROM tbl_already_processed_campaign cal_cp
            WHERE ID_message = 15  -- campaign processed succeeded
              AND cp.id_campaign = cal_cp.id_campaign);

This query has the purpose to validate if the equipment was remunerated in another campaign not associated. 该查询的目的是验证设备是否在另一个未关联的活动中获得了报酬。

Thanks guys! 多谢你们!

EDIT: tbl_campaign table has all the information about a campaign, like ID, equipments, dates, wtv. 编辑: tbl_campaign表具有有关活动的所有信息,例如ID,设备,日期,wtv。

tbl_equip table it has all the information about equipments, id, imei, campaign with the equipment and price. tbl_equip表中包含有关设备,id,imei,活动以及设备和价格的所有信息。

tbl_camp_associates table with associated campaigns. tbl_camp_associates表与相关的广告系列。 It has an rowID, campaign ID, associate campaign ID. 它具有rowID,广告系列ID和关联的广告系列ID。 eg: the campaign 44 has 2 associated campaigns 32 and 33. This results on 2 records on this table. 例如:活动44具有2个关联的活动32和33。此结果基于此表上的2条记录。

EG. 例如。 Campaign 44 with the equipment 1 and 2 associated campaign (32 and 33). 与装备1和2相关联的战役(32和33)的战役44。 Success! 成功! And it was processed on the table tbl_already_processed_campaign with the right message (id_message = 15) 并在表tbl_already_processed_campaign使用正确的消息对其进行了处理(id_message = 15)

EG2: Campaign 45 with the equipment 1 and associated campaign (30 and 31) it must return an error, because it was remunerated on campaign 44 with different associated campaigns. EG2:带有设备1和相关联的活动(30和31)的活动45,它必须返回错误,因为它在具有不同关联活动的活动44中得到了报酬。

EXISTS condition is used in combination with a subquery and is considered to be met if the subquery returns at least one row. EXISTS条件与子查询结合使用,并且如果子查询返回至少一行,则认为满足该条件。 In this case, if the subquery 在这种情况下,如果子查询

select * from tbl_already_processed_campaign cal_cp where ID_message = 15 AND cp.id_campaign= cal_cp.id_campaign

returns at least one row, EXISTS clause will evaluate to true and the condition will be met. 返回至少一行,EXISTS子句的计算结果为true,并且满足条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM