Sql 带条件的查询未返回准确或相关的结果
我有一个表中9条记录的数据集,这是测试数据。 我有下面的数据样本。 在下表中,第一行是标题Sql 带条件的查询未返回准确或相关的结果,sql,ms-access,ms-access-forms,Sql,Ms Access,Ms Access Forms,我有一个表中9条记录的数据集,这是测试数据。 我有下面的数据样本。 在下表中,第一行是标题 +-------------+-------------+----------+---------------+---------------+ | BehrInvoice | TboInvoice | TboRloc | TboDoc | TboPax | +-------------+-------------+----------+---------------+--
+-------------+-------------+----------+---------------+---------------+
| 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
列并获得更准确的结果
===
编辑190906
因此,当我输入:
SELECT * FROM Main
WHERE Main.TboPax LIKE "alva*";
SELECT *
FROM Main
WHERE (((Main.TboPax) Like [Forms]![SearchForm]![PaxName]));
它工作得很好。
当我输入:
SELECT * FROM Main
WHERE Main.TboPax LIKE "alva*";
SELECT *
FROM Main
WHERE (((Main.TboPax) Like [Forms]![SearchForm]![PaxName]));
而[PaxName]=“alva”形式的值,我什么也得不到。可能我引用的表单不正确?第一次查询返回所有内容的原因是,如果
TboInvoice
或PaxName
中的任何一个文本框为空,则[Forms]![搜索表单]![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] & "*"
)
根据所需的行为,如果两个表单控件都为空,则可能需要返回所有记录,这需要第三个条件,例如:
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] & "*"
我猜你只是想要
和:
SELECT *
FROM Main
WHERE (Main.TboInvo LIKE [Forms]![SearchForm]![TboInvoice] & "*") AND
(Main.TboPa LIKE [Forms]![SearchForm]![PaxName] & "*")
如果使用或
并将任一文本框留空,则该文本框的条件将包含所有行。我从不使用动态参数化查询。我希望VBA生成筛选条件并应用于表单或报表。请从“[S]”
中删除括号。不管怎样,如果没有输入,多个动态参数都无法满足您的需要。