我有一个具有ID,First,Last和Identification列的联系人表。试图使名称显示在较大的联接中。连接两列时都无法显示它。

表格:

视察
 ID
 Order_Id
 Inspector_Id

联络人
 ID
 名字
 姓
 订购者
 检验员

这是我的选择:

$sql = " SELECT
        Inspections.Id,
        Inspections.Orderer_Id,
        Inspections.Inspector_Id,
        Contacts.Id
        Contacts.First_Name AS OFirst,
        Contacts.Last_Name AS OLast,
        Contacts.First_Name AS IFirst,
        Contacts.Last_Name AS ILast,

FROM Inspections
JOIN Contacts ON Inspections.Orderer_Id &&         Inspections.Inspector_Id=Contacts.Id
WHERE
                Inspections.Id = " . $trigger . ";";


这只是最新尝试。我已经尝试了多种加入它们的方法,但是我想我在翻译搜索时发现的代码方面遇到了困难。任何帮助将不胜感激!

最佳答案

如果Inspector和Orderer是同一个人,则当前查询仅会起作用(一旦清除语法错误),否则,您必须使用表别名加入两次Contacts:

 SELECT
        Inspections.Id,
        Inspections.Orderer_Id,
        Inspections.Inspector_Id,
        orderer.Id AS OId,
        orderer.First_Name AS OFirst,
        orderer.Last_Name AS OLast,
        inspector.Id AS IId,
        inspector.First_Name AS IFirst,
        inspector.Last_Name AS ILast
FROM Inspections
JOIN Contacts AS orderer ON Inspections.Orderer_Id = orderer.Id
JOIN Contacts AS inspector ON Inspections.Inspector_Id = inspector.Id
WHERE Inspections.Id = xxxx

关于php - MySQL问题加入表与两列反对一,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40182689/

10-13 22:17