我有一个表TBLCUTOMERS,其中包含以下字段:
- cutomerid
- customername
- customerphone
我有另外一个表TBLTRANSACTIONS,其中包含以下字段:
- transactionid
- customerid (foreign key to table above)
- transactiondetail
- transactionamount
我有一个查询来获取来自tbltransactions的所有交易:
"select * from tbltransactions";
我怎么能在其中进行子查询,以便我从TBLCUSTOMERS获取CUSTOMERNAME以反对上面的查询中的每个CUSTMERID?
期待输出:
- transactionid
- customername (from tblcustomers)
- transactiondetail
- transactionamount
请注意,我是MySql的新手。感谢
答案 0 :(得分:1)
select tt.transactionid ,tc.customername,tt.transactiondetail,tt.transactionamount from tbltransactions tt,TBLCUTOMERS tc where tt.customerid=tc.cutomerid
我认为这会解决你的目的。供您参考,请查看link
答案 1 :(得分:0)
它不是子查询,我们为此目的使用join。
等等。Select tt.*, c.customername
from
tbltransactions as tt
left join TBLCUTOMERS as c
on tt.customerid = c.customerid
根据您的要求使用左连接或右连接或内连接
答案 2 :(得分:0)
试试这个:
SELECT T.transactionid, C.customername, T.transactiondetail, T.transactionamount
FROM TBLTRANSACTIONS T
INNER JOIN TBLCUTOMERS C ON T.cutomerid = C.cutomerid;