簡體   English   中英

是否可以通過使用外鍵使用連接子句將兩個表中的其他列名顯示到一個表中

[英]Is it possible to display other column names from two table to one table by using join clause by using foreign key

先生/女士,美好的一天,我需要有關 Join 子句的幫助,是否可以在具有外鍵的其他表的多個列名中使用 join 子句? 問題:我有一個名為
表 1:'biditem'
COL.1:雙端粒(pk)
COL.2: user_id(fk)
COL.3:product_id(fk)
COL.4:bidamountprice(十進制)
COL.5:出價時間(日期時間)

表 2:“用戶”
COL.1: user_id(pk)
COL.2:用戶名(varchar)
COL.3:用戶中間名(varchar)
COL.4:用戶姓氏(varchar)

表 3:“產品”
COL.1:product_id(pk)
COL.2:產品名稱 varchar(25)

我想在 php 中顯示像這樣的 biitem 在此處輸入圖像描述

提到的 2 個表之間確實存在關系,因此您可以嘗試使用別名來引用表以使事情更清晰。

select 
    b.`biditemid`,
    concat(u.`user_firstname`,' ',u.`user_middle`,' ',u.`user_lastname`) as `Name`, 
    p.`product_name` as `Product Name`,
    b.`bidamountprice` as `Bid Amount`,
    b.`bidtime` as `DateTime`
from `bititem` b
    left outer join `user` u on u.`user_id`=b.`user_id`
    left outer join `product` p on p.`product_id`=b.`product_id`
select 
bi.biditemid,
CONCAT(u.user_firstname, ' ', u.user_middle, ' ',u.user_lastname) AS Name,
p.product_name as "Pruduct Name",
bi.bidamountprice as "Bid Amount", 
bi.bidtime as Datetime
from biditem as bi left join user as u on bi.user_id = u.user_id
left join product as p on bi.product_id = p.product_id

之后根據需要添加 where 條件。

您還可以編寫您的查詢,例如

select 
   b.biditemid, 
   concat(u.user_firstname,' ',u.user_middle,' ',u.user_lastname) as name, 
   p.product_name,
   b.bidamountprice, 
   b.bidtime
from 
   bitem b,
   user u,
   product p
where
   b.user_id=u.user_id
   and b.product_id=p.product_id
  • 用別名short form命名您的表
  • 如果您有 3 個表,請始終記住要加入,那么 3-1 將是您與所有表進行正確聯接的最小where子句
  • 同樣,您有 3 個表,因此上述代碼中使用的最小數量或 where 子句為 2。

您可以使用該標准,也可以使用左內、外連接,無論您喜歡什么。這兩個標准都可以在 oracle sqlplus、mySql、postgreSql 中使用

暫無
暫無

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

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