简体   繁体   English

SQL查询(显示所有'x'其中'x'不在表'2'中用于字段'y'并且具有'z'标志)

[英]SQL Query (Display All 'x' Where 'x' Is Not In Table '2' for field 'y' and has 'z' flag)

I need to return all 'contacts' that do not appear in the 'delegate' table for 'event name' but do have flags in the 'contacts' table that can selected by the user for the search. 我需要返回“代理”表中没有出现的“事件名称”的所有“联系人”,但是“联系人”表中的标记可以由用户选择用于搜索。

I know the query can be broken in to 2 parts. 我知道查询可以分为两部分。

  1. Are they already attending this event (Does their email appear in 'delegates' table with delegates.event field matching 'event' on the user form) 他们是否已经参加此活动(他们的电子邮件是否出现在'delegates'表中,其中delegates.event字段匹配用户表单上的'event')
WHERE (
  d.Event <> [Forms]![usf_FindCampaignContacts]![FCC_EventName]
  1. Do they match the criteria (Have they got the HR flag in 'contacts' table) 它们是否符合标准(他们是否在“联系人”表中获得了HR标志)
  AND (c.[HR-DEL]   = [Forms]![usf_FindCampaignContacts]![FCC_HRD]   OR IsNull([Forms]![usf_FindCampaignContacts]![FCC_HRD]));

Based on the 2 things that the query is required to do I have written the following code... 根据查询需要执行的两件事情,我编写了以下代码...

SELECT 
c.[First Name], c.[Last Name], c.Email, d.Event, c.Suppress, c.[HR-DEL]

FROM tbl_Contacts AS c LEFT JOIN tbl_Delegates AS d ON c.Email = d.Email

WHERE (
  d.Event <> [Forms]![usf_FindCampaignContacts]![FCC_EventName]
  And 
  c.Suppress = False
) 
  AND (c.[HR-DEL]   = [Forms]![usf_FindCampaignContacts]![FCC_HRD]   OR IsNull([Forms]![usf_FindCampaignContacts]![FCC_HRD]));

[FCC_HRD] refers to the user selected input on the form, I tried to use a <> to remove matching records but I feel this is where the compile error is so I changed these to and/or statements and this part now returns results with the matching flags (Success) [FCC_HRD]是指用户在表单上选择的输入,我尝试使用<>来删除匹配的记录,但我觉得这是编译错误所在,所以我将这些更改为和/或语句,此部分现在返回结果匹配标志(成功)

Other issue with attempting to do it this way is even if it worked it would remove anyone who was listed in the delegates/sponsor table. 尝试这样做的其他问题是即使它有效也会删除在委托/赞助者表中列出的任何人。 Which is why I added the <> statement for the Event as it only needs to remove them off the list for the named event. 这就是我为事件添加<>语句的原因,因为它只需要从命名事件的列表中删除它们。 Again this works perfectly well (Success) 再次,这非常有效(成功)

Final issue is the results are clearly being pulled from the 'delegates' table not the 'contacts' table as both parts above work but only display the results that match criteria in delegates table not from contacts. 最后一个问题是结果显然是从“代表”表而不是“联系人”表中提取的,因为上述两个部分都起作用,但只显示与代表表中的条件匹配的结果,而不是联系人。

Here is the query/table relationships 这是查询/表关系

Here is the user form (This is not the final design) 这是用户表单 (这不是最终设计)

Below are the 3 tables that are used in the query (2 direct, 1 linked) Contacts (c) 以下是查询中使用的3个表(2个直接,1个链接)联系人(c)

+----+------------+---------------+-------------------------+--------+----------+
| ID | First Name |   Last Name   |          Email          | HR-DEL | Suppress |
+----+------------+---------------+-------------------------+--------+----------+
|  1 | A          | Platt         | a.platt@fake.com        | TRUE   | TRUE     |
|  2 | D          | Farr          | d.farr@fake.com         | TRUE   | FALSE    |
|  3 | Y          | Helle         | y.helle@fake.com        | TRUE   | FALSE    |
|  4 | S          | Oliphant      | soliphant@fake.com      | TRUE   | FALSE    |
|  5 | J          | Bedell-Pearce | jbedell-pearce@fake.com | TRUE   | FALSE    |
|  6 | J          | Walker        | j.walker@fake.com       | FALSE  | FALSE    |
|  7 | S          | Rug           | s.rug@fake.com          | FALSE  | FALSE    |
|  8 | D          | Brown         | d.brown@fake.com        | FALSE  | FALSE    |
|  9 | R          | Cooper        | r.cooper@fake.com       | TRUE   | FALSE    |
| 10 | M          | Morrall       | m.morrall@fake.com      | TRUE   | FALSE    |
+----+------------+---------------+-------------------------+--------+----------+

Delegates (d) 代表们(d)

+----+-------------------------+-------+
| ID |          Email          | Event |
+----+-------------------------+-------+
|  1 | a.platt@fake.com        |     2 |
|  2 | d.farr@fake.com         |     1 |
|  3 | y.helle@fake.com        |     4 |
|  4 | soliphant@fake.com      |     3 |
|  6 | jbedell-pearce@fake.com |     2 |
+----+-------------------------+-------+

Events (not direct but used to check event name drop-down on user form vs event number in delegates) 事件(不是直接但用于检查用户表单上的事件名称下拉列表与代理中的事件编号)

+----+------------+
| ID | Event Name |
+----+------------+
|  1 | Test 1     |
|  2 | Test 2     |
|  3 | Test 3     |
|  4 | Test 4     |
+----+------------+

Based on form selection and this sample data I need to return the following: 根据表单选择和此示例数据,我需要返回以下内容:

All contacts who are flagged 'HR' TRUE, not suppressed or going to event named 'test 2' (Should be 5 - I always return the names of 'delegates' not going to the event only = 3) 标记为“HR”为TRUE,未被抑制或进入名为“test 2”的事件的所有联系人(应该是5 - 我总是返回'代表'的姓名,而不是仅仅返回事件= 3)

Final results should be: 最终结果应该是:

+----+------------+-----------+--------------------+--------+----------+
| ID | First Name | Last Name |       Email        | HR-DEL | Suppress |
+----+------------+-----------+--------------------+--------+----------+
|  2 | D          | Farr      | d.farr@fake.com    | TRUE   | FALSE    |
|  3 | Y          | Helle     | y.helle@fake.com   | TRUE   | FALSE    |
|  4 | S          | Oliphant  | soliphant@fake.com | TRUE   | FALSE    |
|  9 | R          | Cooper    | r.cooper@fake.com  | TRUE   | FALSE    |
| 10 | M          | Morrall   | m.morrall@fake.com | TRUE   | FALSE    |
+----+------------+-----------+--------------------+--------+----------+

At the moment it appears to be pulling results from the wrong table (d not c). 目前它似乎是从错误的表中拉出结果(d不是c)。 I attempted to change to OUTER join type but that returned with a FROM syntax error. 我尝试更改为OUTER连接类型,但返回时出现FROM语法错误。

If I understand it correctly, basically you want to do this: 如果我理解正确,基本上你想这样做:

SELECT A.foo
FROM A 
  LEFT JOIN B 
    ON A.bar = B.bar
WHERE
  <complex condition, partly involving B>

This cannot work. 这不行。 By including B in the global WHERE condition, you turn the LEFT JOIN into an INNER JOIN, and so you will only ever get records that match between A and B. 通过在全局WHERE条件中包含B,可以将LEFT JOIN转换为INNER JOIN,因此您只能获得A和B之间匹配的记录。

You can either move the filter on B into the JOIN condition: 您可以将B上的过滤器移动到JOIN条件:

SELECT A.foo
FROM A 
  LEFT JOIN B 
    ON (A.bar = B.bar)
    AND (B.bamboozle = 42)
WHERE
  A.columns = things

or LEFT JOIN a filtered subquery: 或LEFT JOIN过滤后的子查询:

SELECT A.foo
FROM A 
  LEFT JOIN 
    (SELECT bar, columns FROM B 
     WHERE B.bamboozle = 42) AS B1
    ON A.bar = B1.bar
WHERE
  A.columns = things

So in your query, this is the bamboozle part you will need to move: 所以在你的查询中,这是你需要移动的竹节部分:

d.Event <> [Forms]![usf_FindCampaignContacts]![FCC_EventName]

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

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