【问题标题】:how to use common variable in UNION sql query?如何在 UNION sql 查询中使用公共变量?
【发布时间】:2015-03-02 05:15:23
【问题描述】:

如何设置一个变量以在 SQL 的“transactions.amount > 5”部分中使用,该变量用于以下 SELECT 语句的两个部分?这样我就可以在查询中设置一次值(例如本例中的 5),然后 UNION ALL 语句的两边都可以引用它?

顺便说一下,我使用的是 Microsoft Access,所以这是一个访问查询。

SELECT transactions.title, transactions.amount, categories.title
FROM transactions LEFT JOIN (categories RIGHT JOIN [trans-cat] ON categories.ID = [trans-cat].categoryID) ON transactions.ID = [trans-cat].transactionID
WHERE NOT EXISTS (select transactionID from [trans-cat] where transactions.ID = [trans-cat].transactionID ) AND transactions.amount > 5

union all

SELECT transactions.title, transactions.amount, categories.title
FROM transactions INNER JOIN (categories INNER JOIN [trans-cat] ON categories.ID = [trans-cat].categoryID) ON transactions.ID = [trans-cat].transactionID
WHERE  transactions.amount > 5

【问题讨论】:

  • 我认为您不能在 MS Access 中轻松做到这一点,因为您不能将 union all 放在子查询中。您可以使用union all 创建一个视图,然后在其上使用where 条件。
  • 如果您在两种条件下使用相同的参数名称(即 [threshold]),则需要指定一次参数
  • 感谢@cha - 我什至没有想过要尝试这个(应该有) - 我认为这会帮助我

标签: sql ms-access ms-access-2010


【解决方案1】:

根据@cha 的回复:

如果您对两个条件使用相同的参数名称(即 [阈值]),则需要指定一次参数

【讨论】:

    猜你喜欢
    • 2021-03-09
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 2010-09-17
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多