簡體   English   中英

MySQL查詢以獲取兩個表之間的信息

[英]Mysql query to get info between two tables

我有兩張桌子。

第一個是branches並具有列branchidbranchname等。

第二個表是transfer ,具有fromidtoid列以及其他列(fromid和to id是branchid)

我想要一個查詢來顯示分支的名稱。

現在,我使用兩個查詢來做到這一點:

select branchname
from `transfer`, `branches`
where transfer.fromid = branches.branchid

而另一個查詢是

select branchname
from `transfer`, `branches`
where transfer.toid = branches.branchid
select bfrom.branchname, bto.branchname
from transfer,branches bfrom, branches bto
where transfer.fromid=bfrom.branchid and transfer.toid=bto.branchid

要么

select bfrom.branchname, bto.branchname
from transfer t
inner join branches bfrom on bfrom.branchid=t.fromid
inner join branches bto on bto.branchid=t.toid

我們只需要打一次分支表就可以得到它。

從轉移t中選擇b.branchname,分支b,其中t.fromid = b.branchid或t.toid = b.branchid

暫無
暫無

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

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