【问题标题】:Query with Criteria is not returning accurate or relevant results使用条件查询未返回准确或相关的结果
【发布时间】:2020-01-04 04:12:08
【问题描述】:

我在一个表中有一个包含 9 条记录的数据集,它是测试数据。 我有下面的数据样本。 在下表中,第一行是表头。

+-------------+-------------+----------+---------------+---------------+
| BehrInvoice |  TboInvoice |  TboRloc |     TboDoc    |     TboPax    |
+-------------+-------------+----------+---------------+---------------+
|        4312 |  1449S      |  WIUBLF  |  -0772089627  |  ASARCH/CHAD  |
|        4313 |  1457S      |  TAQXKU  |  XD7366998723 |  CARREON JR/L |
|        4314 |  1457S      |  TAXXKU  |  -7366998723  |  CARREON JR/L |
|        4317 |  1461S      |  TOXSEH  |  XD7366998726 |  ALVA/MICHAEL |
|        4318 |  1460S      |  TOXSEH  |  -7366998726  |  ALVA/MICHAEL |
|        4320 |  1458S      |  ULHHZO  |  -7366998724  |  GREENFIELD/M |
+-------------+-------------+----------+---------------+---------------+

我想做的是能够搜索每一列, 一起。

我希望如果我输入 alva 我会看到 Alva/Michael 记录至少首先弹出。

或者,如果我在 TboInvoice 搜索框中输入 1458,然后在 TboPax 搜索框,我会看到所有这三个记录。

我试图用这个:

SELECT *
FROM Main
WHERE ((Main.TboInvo) LIKE [Forms]![SearchForm]![TboInvoice] & "*")
OR ((Main.TboPax) LIKE [Forms]![SearchForm]![PaxName] & "*")

但是结果集返回了所有内容。 我隔离到 TboInvoice,并尝试了这个:

WHERE ((Main.TboInvo) = [Forms]![SearchForm]![TboInvoice] & "[S]")

它什么也没带回来。

我想我应该只关注TboInvoice,并让它正常运行。

所以,总而言之,问题是:

如何在此处查询TboInvoice 列并获得更准确的结果?

=== 编辑 190906

所以当我输入时:

SELECT * FROM Main​ 
WHERE​ Main.TboPax LIKE "alva*"​;

效果很好。 当我输入时:

SELECT *
FROM Main
WHERE (((Main.TboPax) Like [Forms]![SearchForm]![PaxName]));

和 [PaxName]== "alva" 形式的值,我什么也没得到。也许我错误地引用了表单?

【问题讨论】:

  • 我从不使用动态参数化查询。我更喜欢 VBA 来构建过滤条件并应用于表单或报表。
  • "[S]" 中删除括号。无论如何,如果没有输入,您将无法通过多个动态参数获得所需的内容。

标签: sql ms-access ms-access-forms


【解决方案1】:

我怀疑你只是想要and:

SELECT *
FROM Main
WHERE (Main.TboInvo LIKE [Forms]![SearchForm]![TboInvoice] & "*") AND
      (Main.TboPa LIKE [Forms]![SearchForm]![PaxName] & "*")

如果您使用 OR 并将任一文本框留空,则该文本框的条件将包含所有行。

【讨论】:

    【解决方案2】:

    您的第一个查询返回所有内容的原因是,如果文本框 TboInvoicePaxName 为空,则 [Forms]![SearchForm]![TboInvoice] & "*" 将产生 "*",从而匹配所有记录。

    考虑到这一点,您需要在选择标准中包含一个测试,以说明表单控件何时为空,可能类似于以下内容:

    select * from main
    where
    (
        [Forms]![SearchForm]![TboInvoice] is not null and 
        main.tboinvo like [Forms]![SearchForm]![TboInvoice] & "*"
    ) or
    (
        [Forms]![SearchForm]![PaxName] is not null and 
        main.tbopax like [Forms]![SearchForm]![PaxName] & "*"
    )
    

    根据所需的行为,如果 both 表单控件为空,您可能希望返回 所有 记录,这需要第三个条件,例如:

    select * from main
    where
    (
        [Forms]![SearchForm]![TboInvoice] is not null and 
        main.tboinvo like [Forms]![SearchForm]![TboInvoice] & "*"
    ) or
    (
        [Forms]![SearchForm]![PaxName] is not null and 
        main.tbopax like [Forms]![SearchForm]![PaxName] & "*"
    ) or
    ( 
        [Forms]![SearchForm]![TboInvoice] is null and
        [Forms]![SearchForm]![PaxName] is null
    )
    

    也可以这样写:

    select * from main
    where
    (
        [Forms]![SearchForm]![TboInvoice] is null or
        main.tboinvo like [Forms]![SearchForm]![TboInvoice] & "*"
    ) and 
    (
        [Forms]![SearchForm]![PaxName] is null or 
        main.tbopax like [Forms]![SearchForm]![PaxName] & "*"
    )
    

    注意到Null & "*" 产生"*",这可能变成:

    select * from main
    where
        main.tboinvo like [Forms]![SearchForm]![TboInvoice] & "*"
        and 
        main.tbopax like [Forms]![SearchForm]![PaxName] & "*"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 1970-01-01
      • 2016-11-18
      • 2017-02-21
      相关资源
      最近更新 更多