在 Select SQL 查询中使用 Case Select

发布于 2024-11-02 11:59:29 字数 415 浏览 1 评论 0原文

SELECT
  CASE Forms!FormName!ComboBox
   WHEN  Is Not Null THEN (ParentTable.Column)
     FROM (Parent)
      WHERE (((ParentTable.Column)=Forms!FormName!ComboBox))
  ELSE
   ((ParentTable.Column) FROM Parent END;

好的,我有一个显示父表中记录的表单,我希望能够对其进行过滤。因此,上面的 SQL 代码试图完成的任务是,如果在组合框中进行了选择,则仅选择与该字段匹配的记录。 else 情况是该字段为空,它只选择所有记录。我对 SQL 代码不是很熟悉,所以如果可能的话请解释一下使用的不同参数。

或者我可以使用某种 if 语句来代替吗?

SELECT
  CASE Forms!FormName!ComboBox
   WHEN  Is Not Null THEN (ParentTable.Column)
     FROM (Parent)
      WHERE (((ParentTable.Column)=Forms!FormName!ComboBox))
  ELSE
   ((ParentTable.Column) FROM Parent END;

Okay, I have a form that displays records from the parent table and I want to be able to filter it. So what the SQL code above is trying to accomplish is to say that if there is a selection made in the combo box then select only the records that match that field. The else case is that the field is null and it just selects all records. I am not very familiar with SQL code so please explain different parameters used if possible.

Or is it possible that I could use some sort of if statement instead?

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

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

发布评论

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

评论(3

梦途 2024-11-09 11:59:29

您不能直接在 SQL 中使用表单参数。您可以参数化您的查询并传入一个参数,我们将其称为@FormName:

select
    case
         when @FormName is not null then ParentTable.column1
         else ParentTable.column2
    end
from ParentTable
where ParentTable.column3 = @FormName

You can't use your form parameters directly in SQL. You can parameterize your query and pass in a parameter, let's call it @FormName:

select
    case
         when @FormName is not null then ParentTable.column1
         else ParentTable.column2
    end
from ParentTable
where ParentTable.column3 = @FormName
じ违心 2024-11-09 11:59:29

似乎这里不需要 CASE:

SELECT ParentTable.Column
FROM ParentTable
WHERE (Forms!FormName!ComboBox IS NULL)
   OR (Forms!FormName!ComboBox = ParentTable.Column)

Seems like CASE is not needed here:

SELECT ParentTable.Column
FROM ParentTable
WHERE (Forms!FormName!ComboBox IS NULL)
   OR (Forms!FormName!ComboBox = ParentTable.Column)
燕归巢 2024-11-09 11:59:29

注意:我假设您正在动态绑定 Forms!FormName!ComboBox 参数的值。

对于 MS Access

   SELECT <YOUR_SELECT_LIST>
     FROM ParentTable 
    WHERE NZ(Forms!FormName!ComboBox,  ParentTable.Column) = ParentTable.Column

对于 SQL Server

   SELECT <YOUR_SELECT_LIST>
     FROM ParentTable 
    WHERE ISNULL(Forms!FormName!ComboBox,  ParentTable.Column) = ParentTable.Column

对于 ORACLE

   SELECT <YOUR_SELECT_LIST>
     FROM ParentTable 
    WHERE NVL(Forms!FormName!ComboBox,  ParentTable.Column) = ParentTable.Column        

NOTE: I assume you are binding the value of Forms!FormName!ComboBox parameter dynamically.

For MS Access

   SELECT <YOUR_SELECT_LIST>
     FROM ParentTable 
    WHERE NZ(Forms!FormName!ComboBox,  ParentTable.Column) = ParentTable.Column

For SQL Server

   SELECT <YOUR_SELECT_LIST>
     FROM ParentTable 
    WHERE ISNULL(Forms!FormName!ComboBox,  ParentTable.Column) = ParentTable.Column

For ORACLE

   SELECT <YOUR_SELECT_LIST>
     FROM ParentTable 
    WHERE NVL(Forms!FormName!ComboBox,  ParentTable.Column) = ParentTable.Column        
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文