簡體   English   中英

如何在 sql 的同一個表中加入兩個值?

[英]How to Join the two values in the same table in sql?

這是我附上的樣品表 -樣品表的鏈接

我有一個表名為Customer 在該表中,有幾個屬性,例如 customer_id、customer_name、address、country、bill_to_customer、account_start_date 等……所以我想從該表中獲取計費客戶名稱、地址、國家/地區。 在這里, customer_id可能等於bill_to_customer或 customer_id 不等於 bill_to_customer_id。 當我想獲取計費客戶詳細信息時,它的作用是,go 到 bill_to_customer 值並過濾與 bill_to_customer 匹配的 customer_id 的數據,並給出 output。 如何編寫 SQL 查詢來執行此操作?

您可以使用您的 bill_to_customer 列對表 Customer 進行自聯接。

像這樣的東西:

SELECT billing.name, billing.address, billing.country FROM Customer cust
INNER JOIN Customer billing ON cust.bill_to_customer = billing.customer_id
WHERE ...further conditions...

暫無
暫無

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

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