MySQL:从多个表中检索数据 [英] MySQL: Retrieve data from multiple tables

查看:181
本文介绍了MySQL:从多个表中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看下面的表格结构。


客户端表具有外键 code>对于提供者表,它不是 NULL 。对于客户端表,组合表具有外键这也不是 NULL

我需要从 Portfolio 表中检索所有字段, Client 和提供者分配给客户端的名称,由 Portfolio 表..



如何在SQL代码中执行此操作?

解决方案

使用INNER JOIN尝试以下查询。

  SELECT Portfolio。*, Client.name作为Client Name,Provider.name作为Provider Name
FROM Portfolio
INNER JOIN Client ON Portfolio.Client_id = Client.id
INNER JOIN Provider ON Client.Provider_id = Provider.id


Please have a look at the below table structure.

Client table has the foreign key for Provider Table, which is not NULL. Portfolio table has the foreign key for the Client table, which is also not NULL.

I need to retrieve all the fields from the Portfolio table, Name of the Client and the Provider Name who is allocated to the Client which is referred by the Portfolio table..

How can I do this in SQL Code?

解决方案

Try following query with INNER JOIN.

SELECT Portfolio.*,Client.name as "Client Name",Provider.name as "Provider Name"
FROM Portfolio
INNER JOIN Client ON Portfolio.Client_id=Client.id
INNER JOIN Provider ON Client.Provider_id = Provider.id

这篇关于MySQL:从多个表中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆