如何加入多个表?

时间:2015-04-16 10:13:46

标签: c# mysql

我尝试了两个表的查询,但是如果我想为很多表做这个怎么做呢?

cmd.CommandText = "SELECT *  FROM assignments 
                   inner join Customers on assignments.Customer_ID = customers.Customer_ID";
//assignments and customers are tables

4 个答案:

答案 0 :(得分:1)

考虑代理商,客户,订单作为您的表格&你必须加入他们。

SELECT 
    a.ord_num,
    b.cust_name,
    a.cust_code,  
    c.agent_code,
    b.cust_city  
FROM agents c, customer b, orders a  
WHERE b.cust_city = c.working_area  
    AND a.cust_code = b.cust_code  
    AND a.agent_code = c.agent_code;  

问候!

答案 1 :(得分:1)

这里我举一个例子。您可以创建如下的查询:

select 
    * 
from tblA a 
     inner join tblB b 
         on a.id = b.id 
     inner join tblC 
         on a.id = c.id 
     inner join tblD 
         on a.id = d.id 

答案 2 :(得分:0)

如果您使用的是SQL管理工作室,请右键单击并选择"编辑器中的设计查询"。这是加入表格的最简单方法(它的视觉效果)

答案 3 :(得分:0)

可能会帮助你

SELECT * FROM assignments AS a, customers AS c WHERE a.Customer_ID = c.Customer_ID;