如何使用 And 进行选择查询

发布于 2024-12-18 21:09:22 字数 586 浏览 1 评论 0原文

我想使用 and 条件进行选择查询。

dept, divi, comp as string

Deparment = dept
Division = divi
company = comp

尝试过此查询:

Select *
from table
where
  department = '" & dept & "'
  and division = '" & divi & "'
  and Company = '" & comp & "'

如果 deptdivicomp 值可用,则上述查询有效。

状况

如果用户仅选择部门,则应仅显示该所选部门的所有值
如果用户选择部门和公司意味着,它应该显示所选公司和该公司所选部门
如果用户选择部门、部门和公司意味着,它应该显示所选公司、该公司所选部门、该部门所选部门。

如何进行这个选择查询?

I want to make a select query with an and condition.

dept, divi, comp as string

Deparment = dept
Division = divi
company = comp

Tried this query:

Select *
from table
where
  department = '" & dept & "'
  and division = '" & divi & "'
  and Company = '" & comp & "'

The above query is working if the dept, divi, comp values are available.

Conditions

If user select department only, it should display all value for that selected department only
If user select department and company means, it should display selected company and selected department for that company
If user select department, division and company means, it should display selected company, selected division for that company, selected department for that division.

How to make this select query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小巷里的女流氓 2024-12-25 21:09:22

我认为这可能对你有帮助:

Select *
from table
where
    (@dept = NULL OR department = @dept)
    and (@div = NULL OR division  = @div)
    and (@comp = NULL OR Company = @comp)

I think this may help you:

Select *
from table
where
    (@dept = NULL OR department = @dept)
    and (@div = NULL OR division  = @div)
    and (@comp = NULL OR Company = @comp)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文