MYSQL`SELECT`多个表列,带有`JOIN`和`USING`但不是`*`

时间:2011-10-21 19:48:39

标签: mysql

好的,这就是我正在使用MYSQL SELECT查询。

SELECT * FROM Customers
JOIN CustomerDetails USING (`Customer_ID`)
JOIN CustomerNotes USING (`Customer_ID`)
WHERE Customer_ID ='1'

这很好并且工作得像预期的那样。我想做的事情就是......

SELECT Customers.Customer_ID, Customers.CompanyName, Customers.ContactPerson
FROM Customers
JOIN CustomerDetails.Customer_ID, CustomerDetails.MinPurchase, CustomerDetails.Discount FROM CustomerDetails USING (`Customer_ID`)
JOIN CustomerNotes.Customer_ID, CustomerNotes.CustNotes FROM CustomerNotes USING (`Customer_ID`)
WHERE Customer_ID = '1'

我想要到达这个想法。我希望能够告诉查询要获取的内容而不是*。 我正在使用3个表Customers CustomerDetails CustomerNotes。有任何想法吗???谢谢!

3 个答案:

答案 0 :(得分:1)

SELECT 

    Customers.Customer_ID, 
    Customers.CompanyName, 
    Customers.ContactPerson,

    CustomerDetails.Customer_ID, 
    CustomerDetails.MinPurchase, 
    CustomerDetails.Discount,

    CustomerNotes.Customer_ID, 
    CustomerNotes.CustNotes

FROM Customers
JOIN CustomerDetails USING (`Customer_ID`)
JOIN CustomerNotes USING (`Customer_ID`)
WHERE Customers.Customer_ID = '1'

您必须在每个Customer_ID中使用“as”,因为它们是相同的。但我认为你没有理由选择两次Customer_ID。只需删除重复项即可。最后一个:

SELECT 

    Customers.Customer_ID, 
    Customers.CompanyName, 
    Customers.ContactPerson,

    CustomerDetails.MinPurchase, 
    CustomerDetails.Discount,

    CustomerNotes.CustNotes

FROM Customers
JOIN CustomerDetails USING (`Customer_ID`)
JOIN CustomerNotes USING (`Customer_ID`)
WHERE Customers.Customer_ID = '1'

答案 1 :(得分:1)

您将所需的所有字段放在星星所在的位置:

SELECT Customers.Customer_ID, Customers.CompanyName, Customers.ContactPerson,
CustomerDetails.MinPurchase, CustomerDetails.Discount, CustomerNotes.CustNotes
FROM Customers
JOIN CustomerDetails USING (`Customer_ID`)
JOIN CustomerNotes USING (`Customer_ID`)
WHERE Customer_ID ='1'

答案 2 :(得分:1)

您是要从一个表中选择所有数据,还是从每个表中选择特定列?

   SELECT Customers.* FROM Customers
   JOIN CustomerDetails USING (`Customer_ID`)
   JOIN CustomerNotes USING (`Customer_ID`)
   WHERE Customer_ID ='1'