簡體   English   中英

打印兩個表中的所有字段,並使用 ORACLE CASE 語句基於列將一些列值設置為空

[英]Print all fields from two tables and set some column values to null based on a column using ORACLE CASE statement

我需要打印所有列,但對於某些列,我需要在 Oracle 中使用 CASE 打印空值

| Db fields     | Male    |Female|Trans |
| ------------- | ------- |------|------|
|customer_id    | Y       |      |      |
|customer_name  | Y       |   Y  |  Y   |
|address        |         |   Y  |      |
|city           | Y       |   Y  |  Y   |
|state          | Y       |   Y  |  Y   |
|zip_code       |         |   Y  |      |
|customer_type  | Y       |      |  Y   |

PS-這不是一張桌子

我試圖寫這個查詢-

select
case
     when c.customer_type='Female' then customer_id is null 
     when c.customer_type='Trans' then customer_id is null
case(address)
     when c.customer_type='Male' then address is null 
     when c.customer_type='Trans' then address is null
case(zip_code)
     when c.customer_type='Male' then zip_code is null 
     when c.customer_type='Trans' then zip_code is null
case(customer_type)
    when c.customer_type='Female' then customer_type is null
end
c.customer_id, c.customer_name, c.address, c.city, c.state,c.zip_code,c.customer_type, e.employee_number, e.employee_name from customers c, employees e where e.customer_id=c.customer_id where c.customerid=1;

這是行不通的

================================================== ==========

表和列僅供參考:

CREATE TABLE customers(
  customer_id number(10) NOT NULL,
  customer_name varchar2(50) NOT NULL,
  address varchar2(50),
  city varchar2(50),
  state varchar2(25),
  zip_code varchar2(10),
  customer_type varchar2(10),
  CONSTRAINT customers_pk PRIMARY KEY (customer_id)
);
insert into customers values(1,'A','Japan','Tokyo','J',001,'Male');
insert into customers values(2,'B','UK','London','U',002,'Female');
insert into customers values(3,'C','China','Tibet','S',003,'Trans');
insert into customers values(4,'D','South korea','Souel','S',004,'Female');

CREATE TABLE employees(
  employee_number number(10) NOT NULL,
  employee_name varchar2(50) NOT NULL,
  customer_id number(10),
  CONSTRAINT employees_pk PRIMARY KEY (employee_number),
  CONSTRAINT fk_customers FOREIGN KEY (customer_id) REFERENCES customers(customer_id));


insert into employees values(101,'titu',1);
insert into employees values(102,'kitu',2);
insert into employees values(103,'pitu',3);
insert into employees values(104,'yitu',4);

您可以將輸出打印為由多個串聯列組成的單列。 從您(已刪除)上一個問題中,您指出這是用於標簽的,因此您不需要多列。 在下面的代碼中,根據 customer_type 的值連接了不同的列。

這可以工作:

SELECT 
  CASE 
    WHEN customer_type = 'Male' THEN
      c.customer_id ||' -> '|| c.customer_name ||' -> '|| c.city ||' -> '|| c.state ||' -> '|| c.customer_type  
    WHEN customer_type = 'Female' THEN
      c.customer_name ||' -> '|| c.address ||' -> '|| c.city ||' -> '|| c.state ||' -> '|| c.zip_code  
    WHEN customer_type = 'Trans' THEN
      c.customer_name ||' -> '|| c.city ||' -> '|| c.state ||' -> '|| c.customer_type     
  END as "Output"
  FROM customers c 
       JOIN employees e ON e.customer_id=c.customer_id;


Output                                                                                                                                                                                                   
---------------------------
1 -> A -> Tokyo -> J -> Male
B -> UK -> London -> U -> 2
C -> Tibet -> S -> Trans
D -> South korea -> Souel -> S -> 4

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM