简体   繁体   English

将一个表的多行与另一个表的多列匹配

[英]Match multiple rows one table with multiple columns of another table mysql

I have 2 tables 我有2张桌子

First Table : Contact Columns: 第一表 :联系人列:

Customer ID,Project ID1,Project ID2,Project ID3

so one contact will have one row. 因此一个联系人只有一行。

Second Table: Timesheet Columns 第二张表:时间表列

Project ID, Name, Owner, Hours

One contact can have multiple entries. 一个联系人可以有多个条目。

I want to match project id from time sheet table to 3 columns of contact table. 我想将时间表表中的项目ID与联系人表的3列进行匹配。

I am using following query. 我正在使用以下查询。

SELECT "Customer ID","Project ID","Project Name","Owner",
        "Hours","Approval Status","Status","Project Manager",
        "Sales Person","Account Manager","Discount %","Hourly Rate",
        "Monthly Budget","Total Budget" 
FROM  "Timesheets" 
    LEFT JOIN "Contacts (Boost Media Group)" 
        ON "Timesheets"."Project ID" = "Contacts (Boost Media Group)"."Zoho Projects ID 1";

But it is not returning desired result because I could not check it with Project ID2,Project ID3 of contact table. 但是它没有返回期望的结果,因为我无法使用联系人表的Project ID2,Project ID3进行检查。

Any suggestions how I can solve this? 有什么建议可以解决这个问题吗?

Correct me if I am wrong. 如果我错了,请纠正我。 I guess you want them when any of the condition (Zoho Projects ID 3 or Zoho Projects ID 2 or Zoho Projects ID 1) maches with "Timesheets"."Project ID" 我猜想任何条件(Zoho项目ID 3或Zoho项目ID 2或Zoho项目ID 1)与“时间表”匹配时,您都需要它们。“项目ID”

SELECT "Customer ID","Project ID","Project Name","Owner",
        "Hours","Approval Status","Status","Project Manager",
        "Sales Person","Account Manager","Discount %","Hourly Rate",
        "Monthly Budget","Total Budget" 
FROM  "Timesheets" 
    LEFT JOIN "Contacts (Boost Media Group)" 
        ON "Timesheets"."Project ID" = "Contacts (Boost Media Group)"."Zoho Projects ID 1"
or "Timesheets"."Project ID" = "Contacts (Boost Media Group)"."Zoho Projects ID 2"
or "Timesheets"."Project ID" = "Contacts (Boost Media Group)"."Zoho Projects ID 3"

Try This Query 试试这个查询

SELECT `(Boost Media Group)`.`Customer ID`,`tyshet`.`Project ID`,`tyshet`.`Project Name`,`tyshet`.`Owner`,
        `tyshet`.`Hours`,`tyshet`.`Approval Status`,`tyshet`.`Status`,`tyshet`.`Project Manager`,
        `tyshet`.`Sales Person`,`tyshet`.`Account Manager`,`tyshet`.`Discount %`,`tyshet`.`Hourly Rate`,
        `tyshet`.`Monthly Budget`,`tyshet`.`Total Budget`
FROM  Timesheets as `tyshet`
    LEFT JOIN Contacts as `(Boost Media Group)`
        ON `tyshet`.`Project ID` = `(Boost Media Group)`.`Zoho Projects ID 1`;

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

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