简体   繁体   English

一个SQL查询字符串中的两个内部联接

[英]Two inner joins in one SQL query string

I'm currently trying to get information from three tables. 我目前正在尝试从三个表中获取信息。 I have a 'worker' table which includes a store ID (store_idStore) and a job ID (job_idJob) as foreign keys. 我有一个'worker'表,其中包含一个商店ID(store_idStore)和一个工作ID(job_idJob)作为外键。 In the Store and Job tables, each of them has a 'Name' field - this is the data I want to return in my query along with all of the information from Worker. 在商店和工作表中,每个表都有一个“名称”字段-这是我想在查询中返回的数据以及来自Worker的所有信息。 My only caveat is that I have to put the query into a string in C#. 我唯一的警告是我必须将查询放入C#中的字符串中。

So far I'm able to join and get the information from Worker and Store: 到目前为止,我已经能够加入并从Worker和Store获取信息:

SELECT worker.*, store.Name AS 'Store' 
from worker inner join store on worker.Store_idStore = store.idStore

How do I expand this to get the relative Job's name as well? 如何扩展此名称以获取相对的Job名称?

试试这个:

SELECT worker.*,store.Name AS store,job.Name AS job from worker inner join store on worker.Store_idStore = store.idStore and worker inner join job on worker.job_idJob = job.idJob

Just append another inner join on the end 只需在末尾附加另一个内部联接

SELECT w.*,s.Name AS 'Store', j.name as 'job' from worker w
inner join store s on worker.Store_idStore = store.idStore
Inner join job J on worker.job_idjob = job.idjob

Try this: 尝试这个:

SELECT DISTINCT w.*, s.*, j.*
FROM worker AS w
 LEFT JOIN store AS s ON w.store_idStore = s.id
 LEFT JOIN job AS j ON w.job_idJob = j.id

Of course you can replace * with any columns' names you want. 当然,您可以将*替换为所需的任何列名称。 And you can add Where clause at the end. 您可以在末尾添加Where子句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM