简体   繁体   English

当表或视图具有值时,如何仅尝试 select 数据

[英]How do I only attempt to select data from a table or view when it has a value

The select statement below returns no rows at the moment because sometimes A.ACCOUNT_OWNER_ID = P.PERSON_ID is false.下面的 select 语句目前不返回任何行,因为有时 A.ACCOUNT_OWNER_ID = P.PERSON_ID 为假。 I'm looking for a way to ONLY select the P.FIRST_NAME when these 2 fields match.当这两个字段匹配时,我正在寻找一种仅 select P.FIRST_NAME 的方法。 If they don't I still need to select the other fields.如果他们不这样做,我仍然需要 select 其他字段。

SELECT DISTINCT S.PRODUCT_NUMBER    AS PRODUCT_NUMBER,
       A.ACCOUNT_ID        AS ACCOUNT_ID,
       AA.COUNTRY_CODE     AS COUNTRY_CODE,
       P.FIRST_NAME        AS FIRST_NAME
       FROM VW_SUBSCRIPTION S, VW_ACCOUNT A, VW_ACCOUNT_ADDRESS AA,VW_PERSON P
       WHERE S.ACCOUNT_ID = A.ACCOUNT_ID
       AND A.ACCOUNT_OWNER_ID = P.PERSON_ID
       AND S.ACCOUNT_ID = AA.ACCOUNT_ID;

I tried doing a join like this:我试着做这样的加入:

SELECT DISTINCT S.PRODUCT_NUMBER AS PRODUCT_NUMBER, 
    A.ACCOUNT_ID AS ACCOUNT_ID, 
    AA.COUNTRY_CODE AS COUNTRY_CODE, 
    P.FIRST_NAME AS FIRST_NAME 
    FROM VW_SUBSCRIPTION S, VW_ACCOUNT A, VW_ACCOUNT_ADDRESS AA 
    JOIN VW_PERSON P 
    ON A.ACCOUNT_OWNER_ID = P.PERSON_ID 
    WHERE S.ACCOUNT_ID = 
    A.ACCOUNT_ID AND S.PRICE_PLAN = 'dealer' AND S.ACCOUNT_ID = 
    AA.ACCOUNT_ID;

But got this error:但是得到了这个错误:

ORA-00904: "A"."ACCOUNT_OWNER_ID": invalid identifier 00904. 00000 - "%s: invalid identifier" *Cause: ORA-00904: "A"."ACCOUNT_OWNER_ID": 无效标识符 00904. 00000 - "%s: 无效标识符" *原因:
*Action: Error at Line: 7 Column: 17 *操作:行错误:7 列:17

I think you need a LEFT join for the table VW_PERSON , so unmatched rows will also be returned (and proper join syntax for the other tables):我认为您需要表VW_PERSONLEFT连接,因此还将返回不匹配的行(以及其他表的正确连接语法):

SELECT DISTINCT 
       S.PRODUCT_NUMBER AS PRODUCT_NUMBER, 
       A.ACCOUNT_ID AS ACCOUNT_ID, 
       AA.COUNTRY_CODE AS COUNTRY_CODE, 
       P.FIRST_NAME AS FIRST_NAME 
FROM VW_SUBSCRIPTION S
INNER JOIN VW_ACCOUNT A ON S.ACCOUNT_ID = A.ACCOUNT_ID
INNER JOIN VW_ACCOUNT_ADDRESS AA ON S.ACCOUNT_ID = AA.ACCOUNT_ID
LEFT JOIN VW_PERSON P ON A.ACCOUNT_OWNER_ID = P.PERSON_ID 
WHERE S.PRICE_PLAN = 'dealer';

You would left join the table/view VW_PERSON with VW_ACCOUNT in order to get records when there are no matches between VW_PERSON and VW_ACCOUNT as follows当 VW_PERSON 和 VW_ACCOUNT 之间没有匹配项时,您可以将表/视图 VW_PERSON 与 VW_ACCOUNT 连接起来,以便获取记录,如下所示

   SELECT DISTINCT 
          S.PRODUCT_NUMBER    AS PRODUCT_NUMBER
         ,A.ACCOUNT_ID        AS ACCOUNT_ID
         ,AA.COUNTRY_CODE     AS COUNTRY_CODE
         ,P.FIRST_NAME        AS FIRST_NAME
     FROM VW_SUBSCRIPTION S
     JOIN VW_ACCOUNT A
       ON S.ACCOUNT_ID = A.ACCOUNT_ID
     JOIN VW_ACCOUNT_ADDRESS AA
       ON S.ACCOUNT_ID = AA.ACCOUNT_ID
LEFT JOIN VW_PERSON P
      ON A.ACCOUNT_OWNER_ID = P.PERSON_ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我需要 select 值时,如何从表中区分值 select 如果它同时具有 y、n 值和 n 值,则只有在没有 y 值的情况下 - how to select distinct values from a table when i need to select values having y if it has both y,n values and n values only if there is no y value 如何过滤表中的值,但仅当它具有记录时? - How do I filter on values in a table, but only when it has records? 如何根据跨国表中的值仅选择一列结果? - How do I select only one column result based on its value from a transnational table? 如何使用外部应用仅从一张表中选择数据? - How do I select data only from one table using outer apply? 如何从中创建视图 select,并将其与现有表连接 - How do I create a view, select from it, and join it with an existing table 在读取视图数据时如何添加选择参数? - How do I add select parameters when reading data for the View? 如何从表格中仅选择月末日期 - How do I select only the month end date from a table 如何根据排名值从表中选择行 - How do I SELECT rows from a table based on a rank value 如何在PostgreSQL的子表中选择数据? - How do I SELECT data from child table in PostgreSQL? select 如何仅从具有所有刷卡记录的表中刷卡用户 - How do select only swipe-in users from the table which has all swipe records
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM