【问题标题】:Linking a table to a second table and pulling data from the first one to the second one multiple times将一个表链接到第二个表并将数据从第一个表多次提取到第二个表
【发布时间】:2012-08-13 04:43:27
【问题描述】:

我有 2 张桌子,1 张用于位置,1 张用于员工。在位置表中,我有 3 个字段:contact1、contact2 和 partner。我需要这些中的每一个从员工表中选择姓名、电子邮件和电话号码并显示出来。我似乎只能让它一次拉一个联系人。这是我所拥有的

SELECT officelocations_tbl.*, staff_tbl.*, city_tbl.*, state_tbl.*
FROM officelocations_tbl
JOIN city_tbl ON officelocations_tbl.cityID = city_tbl.cityID
JOIN state_tbl ON officelocations_tbl.stateID = state_tbl.stateID
JOIN staff_tbl ON staff_tbl.staffID = officelocations_tbl.contact1

那只显示办公室信息和我希望它做这样的事情的一个联系人

SELECT officelocations_tbl.*, staff_tbl.*, city_tbl.*, state_tbl.*
FROM officelocations_tbl
JOIN city_tbl ON officelocations_tbl.cityID = city_tbl.cityID
JOIN state_tbl ON officelocations_tbl.stateID = state_tbl.stateID
JOIN staff_tbl ON staff_tbl.staffID = officelocations_tbl.contact1
JOIN staff_tbl ON staff_tbl.staffID = officelocations_tbl.contact2
JOIN staff_tbl ON staff_tbl.staffID = officelocations_tbl.partner

但是这样做会给我一个错误

警告:mysql_fetch_assoc():提供的参数不是......中的有效 MySQL 结果资源

还有其他方法可以通过使用 staffID 并将其链接到另一个表中的三个不同字段来列出它们吗?

我在这里How to select data from multiple tables using joins/subquery properly? (PHP-MySQL) 尝试了解决方案,但它无法识别 from 部分中的第二个 select 语句。我还尝试了 concat ,它说它不是有效的 sql 查询,内部连接也是如此。我正在使用 php/mysql 数据库。我在上面发布的消息是我在使用该页面上的任何示例时不断收到的消息。唯一改变的是错误所在的行。

我想只创建 4 个单独的 sql 语句。我知道有一种方法可以做到这一点,但我尝试过的方法似乎不起作用。

感谢您的帮助。

在下方提供帮助后编辑

好的,所以我让它显示,但有一个小问题,当我告诉它显示 sf1 的联系信息时,它只显示 2 个条目,而应该有 13 个条目。我总共有 29 个我想成为的位置能够显示并非所有位置都有联系人 1 或联系人 2,但都有合作伙伴。这是我为反映您的建议而编辑的代码:

    $sql_locations = "SELECT officelocations_tbl.*, 
                  sf1.firstName AS c1Firstname, sf1.lastName AS c1lastName, sf1.middleInitial AS c1middleInitial, sf1.suffix AS c1suffix, sf1.accredations AS c1accredations,
                  sf2.firstName AS c2Firstname, sf2.lastName AS c2lastName, sf2.middleInitial AS c2middleInitial, sf2.suffix AS c2suffix, sf2.accredations AS c2accredations,
                  sf3.firstName AS c3Firstname, sf3.lastName AS c3lastName, sf3.middleInitial AS c3middleInitial, sf3.suffix AS c3suffix, sf3.accredations AS c3accredations,
                  city_tbl.*, state_tbl.*
                FROM officelocations_tbl
                  JOIN city_tbl ON (officelocations_tbl.cityID = city_tbl.cityID)
                  JOIN state_tbl ON (officelocations_tbl.stateID = state_tbl.stateID)
                  JOIN staff_tbl sf1 ON (sf1.staffID = officelocations_tbl.contact1)
                  JOIN staff_tbl sf2 ON (sf2.staffID = officelocations_tbl.contact2)
                  JOIN staff_tbl sf3 ON (sf3.staffID = officelocations_tbl.partner)";
                $result_loc = mysql_query($sql_locations);

                while ($db_field = mysql_fetch_assoc($result_loc)) {
                    if ($db_field['c2Firstname'] == ""){
                        print $db_field['officeName'] . "<BR>";
                        print $db_field['address1'] . "<BR>";
                        print $db_field['cityName'] . ", " . $db_field['state_abreviation'] . " " . $db_field['zipCode']."<BR>";
                        print $db_field['c1Firstname'] . " " . $db_field['c1lastName'] . " ". $db_field['c1middleInitial'] . " ". $db_field['c1suffix']. " ". $db_field['c1accredations'] . "<BR><BR><BR><BR>";
                        print $db_field['c3Firstname'] . " " . $db_field['c3lastName'] . " ". $db_field['c3middleInitial'] . " ". $db_field['c3suffix']. " ". $db_field['c3accredations'] . "<BR>";
                    }else if ($db_field['c2Firstname'] != ""){
                        print $db_field['officeName'] . "<BR>";
                        print $db_field['address1'] . "<BR>";
                        print $db_field['cityName'] . ", " . $db_field['state_abreviation'] . " " . $db_field['zipCode']."<BR>";
                        print $db_field['c1Firstname'] . " " . $db_field['c1lastName'] . " ". $db_field['c1middleInitial'] . " ". $db_field['c1suffix']. " ". $db_field['c1accredations'] . "<BR>";
                        print $db_field['c2Firstname'] . " " . $db_field['c2lastName'] . " ". $db_field['c2middleInitial'] . " ". $db_field['c2suffix']. " ". $db_field['c2accredations'] . "<BR>";
                        print $db_field['c3Firstname'] . " " . $db_field['c3lastName'] . " ". $db_field['c3middleInitial'] . " ". $db_field['c3suffix']. " ". $db_field['c3accredations'] . "<BR><BR><BR><BR>";

                    }

我确实尝试让 if 语句说

    if ($db_field['c1Firstname'] != "" && $db_field['c2Firstname'] == "")

但它似乎也不起作用。

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    您在没有后缀的情况下多次加入 staff_tbl,试试这样:

    SELECT officelocations_tbl.*, 
      sf1.FirstName AS c1Firstname, 
      sf2.FirstName AS c2Firstname, 
      sf3.FirstName AS partnerFirstname, 
      city_tbl.*, state_tbl.*
    FROM officelocations_tbl
      JOIN city_tbl ON (officelocations_tbl.cityID = city_tbl.cityID)
      JOIN state_tbl ON (officelocations_tbl.stateID = state_tbl.stateID)
      LEFT OUTER JOIN staff_tbl sf1 ON (sf1.staffID = officelocations_tbl.contact1)
      LEFT OUTER JOIN staff_tbl sf2 ON (sf2.staffID = officelocations_tbl.contact2)
      LEFT OUTER JOIN staff_tbl sf3 ON (sf3.staffID = officelocations_tbl.partner)
    

    当然,您必须为来自不同JOINS 的所有列添加 sf1.[column2] AS c1Column2、sf2.[column2] AS c2Column2、sf3.[column2] AS partnerColumn2 等等。

    您可以在运行mysql_query() 后立即使用echo mysql_error(); 查看您的确切错误

    其他读者注意:我已根据以下问题编辑了我的答案

    【讨论】:

    • 这种作品我将如何告诉它打印?我有这样的东西: print $db_field['firstName'] 。 “”。 $db_field['middleInitial'] 。 “”。 $db_field['姓氏'];我会这样做:db_field['sf1.firstName']?
    • 所以我应该可以像 $db_field['c1lastName'] 一样打印它吗?
    • 是的,如果您在查询中输入sf1.lastName AS c1lastName,但我建议您试一试,并尝试根据您的需要调整我的查询。
    • 我再次编辑了我的答案,请注意查询中的LEFT OUTER JOIN,而不是普通的JOIN
    • 由于某种原因,它只提取了大约一半的位置。你知道它为什么会这样做吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    相关资源
    最近更新 更多