MySQL: Retrieve data from multiple tables

五迷三道 提交于 2019-12-12 04:46:31

问题


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?


回答1:


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



回答2:


This should give you the result you expected:

Select * from client 
  join portfolio on client.id=portfolio.clientId
  join provider on client.provider_id=provider.id


来源:https://stackoverflow.com/questions/25763379/mysql-retrieve-data-from-multiple-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!