1

我有 2 个 mysql 语句,其中一个从 4 个链接表中获取所有信息以供员工使用。我正在尝试获取另一个表中的每个工作人员的标题,但我无法添加到 1 语句,因为它与选择的主表无关。我在 mysql 语句上尝试了大约 20 种变体,但均未成功。我确实让它拉出标题并通过第二个语句中的变量输出它,但它给每个人一个相同的标题,当它再次循环时似乎没有更新变量。

第一条 mysql 语句读取(简化版很长)

    SELECT staff_tbl.titleID FROM staff_tbl

第二条mysql语句读取

    SELECT titles_tbl.titleID, titles_tbl.titleName FROM titles_tbl WHERE titles_tbl.titleID = '" . $getTitle . "'"

变量设置为($row_rsOfficeLocation 是第一个 mysql 语句的结果)

    $getTitle = $row_rsOfficeLocation['titleID'];

有谁知道如何让它更新 $getTitle 变量,当它在第二个语句中每次在第一个语句中更新时被请求时?

编辑:这是数据库结构和一些值

设置 FOREIGN_KEY_CHECKS=0;


-- 表结构city_tbl


如果存在则删除表city_tbl;CREATE TABLE city_tbl( cityIDint(11) NOT NULL auto_increment, cityNamevarchar(255) NOT NULL, PRIMARY KEY ( cityID)) ENGINE=MyISAM AUTO_INCREMENT=83 DEFAULT CHARSET=utf8;


-- city_tbl 的记录


插入city_tbl值('1','Aloha');插入city_tbl值('2','安娜堡');


-- 表结构officelocations_tbl


如果存在则删除表officelocations_tbl;CREATE TABLE officelocations_tbl( locationIDint(11) NOT NULL auto_increment, officeNamechar(255) NOT NULL, address1varchar(285) NOT NULL, address2varchar(285) 默认 NULL, cityIDint(11) NOT NULL, stateIDint(11) NOT NULL, zipCodeint(11)默认 NULL, officePhonevarchar(13) 默认 NULL, contact1int(11) 默认 NULL, contact2int(11) 默认 NULL, partnerint(11) 默认 NULL, PRIMARY KEY ( locationID), KEY City( cityID), KEY State( stateID), KEY Contact1( contact1), KEY Contact2( contact2), KEY Partner( partner) ) 引擎=MyISAM AUTO_INCREMENT=31 默认字符集=utf8;


-- officelocations_tbl 记录


插入officelocations_tbl值('1','Atlanta','5555 Some Ave','#311','3','10','30041','',null,null,'26');插入officelocations_tbl值('2','Austin','5555 Some Ave','','4','43','78734','555-555-1212','72',null,'81' );


-- 表结构staff_tbl


如果存在则删除表staff_tbl;CREATE TABLE staff_tbl( staffIDint(11) NOT NULL auto_increment, staffID_C2int(11) NOT NULL, staffID_Pint(11) NOT NULL, firstNamevarchar(255) NOT NULL, middleInitialvarchar(15) 默认 NULL, lastNamevarchar(255) NOT NULL, suffixvarchar(15)默认 NULL, accredationsvarchar(150) 默认 NULL, emailvarchar(255) NOT NULL, phonevarchar(25) NOT NULL, mobilePhonevarchar(25) 默认 NULL, officePhonevarchar(25) 默认 NULL, faxNumbervarchar(25) 默认 NULL, address1varchar(255) NOT NULL, address2varchar(255) 默认 NULL, cityIDint(11) NOT NULL, stateIDint(11) NOT NULL, zipCodeint(11) NOT NULL, titleIDint(11) NOT NULL, locationIDint(11) NOT NULL, photoURLvarchar(255) 默认 NULL, vCardURLvarchar(255) 默认 NULL, qRCodeURLvarchar(255) 默认 NULL, resumeURLvarchar(255) 默认 NULL, biographylongtext, dateCreateddate NOT NULL, PRIMARY KEY ( staffID), KEY State( stateID), KEY Location( locationID), KEY TitleUSING BTREE ( titleID), KEY CityUSING BTREE ( cityID), KEY StaffUSING BTREE ( staffID) ) ENGINE=MyISAM AUTO_INCREMENT=102 DEFAULT CHARSET=utf8;


-- staff_tbl 的记录


插入staff_tbl值('1','1','1','John','','Doe','','','johndoe@someemail.com','5555551212','','5555551212 ', '5555551212', '5555 Some Ave.', 'Suite 8, #317', '21', '42', '55555', '3', '0', '', '', '', '', '1', '2012-08-02'); 插入staff_tbl值('2','2','2','Jane','','Doe','','','jandoe@someemail.com','5555551212','','5555551212 ', '5555551212', '555555 SW Some Ave', '', '1', '37', '55555', '3', '0', '', '', '',


-- 表结构state_tbl


如果存在则删除表state_tbl;CREATE TABLE state_tbl( stateIDint(11) NOT NULL auto_increment, state_abreviationchar(10) NOT NULL, state_namechar(100) NOT NULL, PRIMARY KEY ( stateID) ) ENGINE=MyISAM AUTO_INCREMENT=54 DEFAULT CHARSET=utf8;


-- state_tbl 的记录


插入state_tbl值('51'、'DC'、'Washington DC');插入state_tbl值('1','AL','Alabama');


-- 表结构titles_tbl


如果存在则删除表titles_tbl;CREATE TABLE titles_tbl( titleIDint(11) NOT NULL auto_increment, titleNamevarchar(255) NOT NULL, PRIMARY KEY ( titleID)) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;


--titles_tbl 的记录


INSERT INTO titles_tblVALUES ('1', 'Associate Consultant\r\n'); INSERT INTO titles_tblVALUES(“2”、“首席财务官”);插入titles_tbl值('3','顾问');INSERT INTO titles_tblVALUES ('4', 'Director, Business Development'); 插入titles_tbl值('5','保留标题空白');INSERT INTO titles_tblVALUES('6','国家信息技术总监');INSERT INTO titles_tblVALUES('7','国家运营总监');INSERT INTO titles_tblVALUES('8','国家技术总监');插入titles_tbl值('9','合作伙伴');INSERT INTO titles_tblVALUES ('10', '区域顾问'); 插入titles_tbl值(' 11', '高级顾问'); titles_tblINSERT INTO VALUES(“12”、“副总裁”);插入titles_tbl价值观('13','项目会计师');插入titles_tbl值('14','');

示例输出如下:

样本输出 它正在工作,除了它输出的所有头衔对于每个工作人员都是相同的。我使用的代码是我之前为第二个 select 语句发布的代码。我不知道如果我能弄清楚如何将titles_tbl加入主sql语句是否会更容易。我已经尝试了从联合到第一个中的第二个选择的几种变体,但它们似乎都不起作用或者它们抛出错误。

这是我一直在使用的第一个 sql 语句,包括我的连接的所有别名

    SELECT staff_tbl.staffID, staff_tbl.staffID_C2, staff_tbl.staffID_P, staff_tbl.firstName, staff_tbl.middleInitial, staff_tbl.lastName, staff_tbl.suffix, staff_tbl.accredations, staff_tbl.email, staff_tbl.phone, staff_tbl.mobilePhone, staff_tbl.officePhone, staff_tbl.faxNumber, staff_tbl.address1, staff_tbl.address2, staff_tbl.cityID, staff_tbl.stateID, staff_tbl.zipCode, staff_tbl.titleID, staff_tbl.locationID, staff_tbl.photoURL, staff_tbl.vCardURL, staff_tbl.qRCodeURL, staff_tbl.resumeURL, staff_tbl.biography, staff_tbl.dateCreated, officelocations_tbl.locationID, officelocations_tbl.officeName, officelocations_tbl.address1, officelocations_tbl.address2, officelocations_tbl.cityID, officelocations_tbl.stateID, officelocations_tbl.zipCode, officelocations_tbl.officePhone, officelocations_tbl.contact1, officelocations_tbl.contact2, officelocations_tbl.partner, city_tbl.cityID, city_tbl.cityName, state_tbl.stateID, state_tbl.state_abreviation, state_tbl.state_name, titles_tbl.titleID, titles_tbl.titleName,  contact1.firstName AS c1Firstname, contact1.lastName AS c1lastName, contact1.middleInitial AS c1middleInitial, contact1.suffix AS c1suffix,  contact1.accredations AS c1accredations, contact1.phone AS c1Phone, contact1.faxNumber AS c1FaxNumber, contact1.mobilePhone AS c1Mobile,  contact1.email AS c1Email, contact1.titleID AS c1Title,  contact2.firstName AS c2Firstname, contact2.lastName AS c2lastName, contact2.middleInitial AS c2middleInitial, contact2.suffix AS c2suffix,  contact2.accredations AS c2accredations, contact2.phone AS c2Phone, contact2.faxNumber AS c2FaxNumber, contact2.mobilePhone AS c2Mobile,  contact2.email AS c2Email, contact2.titleID AS c2Title,  partner.firstName AS c3Firstname, partner.lastName AS c3lastName, partner.middleInitial AS c3middleInitial, partner.suffix AS c3suffix,  partner.accredations AS c3accredations, partner.phone AS c3Phone, partner.faxNumber AS c3FaxNumber, partner.mobilePhone AS c3Mobile,  partner.email AS c3Email, partner.titleID AS c3Title  FROM officelocations_tbl INNER JOIN staff_tbl ON staff_tbl.staffID = officelocations_tbl.contact1 INNER JOIN state_tbl ON state_tbl.stateID = officelocations_tbl.stateID INNER JOIN titles_tbl ON titles_tbl.titleID = staff_tbl.titleID INNER JOIN city_tbl ON city_tbl.cityID = officelocations_tbl.cityID LEFT OUTER JOIN staff_tbl contact1 ON (contact1.staffID = officelocations_tbl.contact1)  LEFT OUTER JOIN staff_tbl contact2 ON (contact2.staffID = officelocations_tbl.contact2)  LEFT OUTER JOIN staff_tbl partner ON (partner.staffID = officelocations_tbl.partner) 

很抱歉这段代码很长,但我需要一个小时才能缩进。

编辑:好的,所以我重新编写了我的 mysql 语句并让它现在从数据库中提取所有信息。但我有一个小问题。在循环的每 3 次迭代中,我都会切换我的联系人 1 和合作伙伴在列表中的位置。似乎只是将其中两个切换到了故障状态。因此,如果我有 3 个触点,它会切换触点 1 和触点 2,而在我只有 2 个触点的触点上,它会切换它们的顺序。

这是我正在使用的新 mysql 语句。

    SELECT staff_tbl.staffID, staff_tbl.firstName, staff_tbl.middleInitial, staff_tbl.lastName,
     staff_tbl.suffix, staff_tbl.accredations, staff_tbl.email, staff_tbl.phone,
     staff_tbl.mobilePhone, staff_tbl.officePhone, staff_tbl.faxNumber, staff_tbl.titleID, 
    staff_tbl.locationID, titles_tbl.titleID, titles_tbl.titleName,    
    officelocations_tbl.locationID, officelocations_tbl.officeName, officelocations_tbl.address1,
     officelocations_tbl.address2, officelocations_tbl.cityID, officelocations_tbl.stateID, 
    officelocations_tbl.zipCode, officelocations_tbl.officePhone, officelocations_tbl.contact1,
     officelocations_tbl.contact2, officelocations_tbl.partner, state_tbl.stateID, 
    state_tbl.state_abreviation, state_tbl.state_name, city_tbl.cityID, city_tbl.cityName, 
    officelocations_tbl.contact1 AS c1Contact, officelocations_tbl.contact2 AS c2Contact, 
    officelocations_tbl.partner AS c3Contact FROM staff_tbl INNER JOIN titles_tbl ON 
    titles_tbl.titleID = staff_tbl.titleID INNER JOIN officelocations_tbl ON officelocations_tbl.contact1 = staff_tbl.staffID  OR officelocations_tbl.contact2 = staff_tbl.staffID
      OR officelocations_tbl.partner = staff_tbl.staffID  
    OR staff_tbl.locationID = officelocations_tbl.locationID 
    INNER JOIN state_tbl ON state_tbl.stateID = officelocations_tbl.stateID 
    INNER JOIN city_tbl ON city_tbl.cityID = officelocations_tbl.cityID

对于我的显示,我有一个 if 语句来检查是否有 contact1、contact2 和合作伙伴。如果联系人 2 为空,它将仅显示联系人 1 和合作伙伴,而如果联系人 2 存在,它将显示所有 3 个联系人,如果没有联系人 1 或联系人 2,则将仅显示合作伙伴的 else 语句。通过这个循环的php完美地工作,所以没有错误。是对和/或还不太正确的 sql 语句的引用导致了我的错误。对于每个循环,我允许语句运行,并且当它遇到第二个(如果有)联系人时再次获取该行。我设置了一个等于 staff_id 的变量,这样我就可以让它拉出与每个位置相关联的正确人员。在我获取行之前,我将员工 ID 设置为特定的联系人示例:

    $staff_ID == $row_rsOfficeLocation['c3Contact'];

它正确读取了分配,但由于某种原因,它似乎在读取到员工 ID 的分配之前就已经调用了 fetch assoc 调用。有效地将员工 ID 设置为列表中的第二个名称,而不是第一个。

有任何想法吗?

编辑以包含 php 循环:

    <?php do { 
$staff_ID = $row_myQuery['staffID'];
?>        
    <ul>
              <li><?php echo $row_myQuery['address1']; ?> <?php echo $row_myQuery['address2']; ?></li>
              <li><?php echo $row_myQuery['cityName']; ?>, <?php echo $row_myQuery['state_abreviation']; ?> <?php echo $row_myQuery['zipCode']; ?></li>
              <br />


              <?php
                if ($row_myQuery['c2Contact'] == "" && $row_myQuery['c1Contact'] != ""){?>
                    <!-- if contact 2 is empty display only contact 1 and partner -->

                     <!-- contact 1 information -->  
                     <?php if ($row_myQuery['c1Contact'] != ""){    
                     echo $staff_ID;                      
                         $staff_ID == $row_myQuery['c1Contact'];?>
                        <li class="emailGrey"><strong><?php echo $row_myQuery['firstName']; ?> <?php echo $row_myQuery['middleInitial']; ?> <?php echo $row_myQuery['lastName']; ?> <?php echo $row_myQuery['suffix']; ?></strong></li>
                        <li>Title: <?php echo $row_myQuery['titleName']; ?></li>
                          <li>Tel: <?php echo format_phone($row_myQuery['phone']);?></li>
                          <li>Fax: <?php echo format_phone($row_myQuery['faxNumber']); ?></li>
                          <li><a href="mailto:<?php echo $row_myQuery['email']; ?>">Email: <?php echo $row_myQuery['email'];  echo $row_myQuery['staffID']; ?></a></li>
                          <br />
                       <?php }
                   ?>
                       <?php if ($row_myQuery['c3Contact'] != ""){  

                         $staff_ID == $row_myQuery['c3Contact'];
                         $row_myQuery = mysql_fetch_assoc($myQuery);
                          echo $staff_ID;   ?>

                          <!-- partner information -->  
                          <li class="emailGrey"><strong><?php echo $row_myQuery['firstName']; ?> <?php echo $row_myQuery['middleInitial']; ?> <?php echo $row_myQuery['lastName']; ?> <?php echo $row_myQuery['suffix']; ?></strong></li>
                          <li>Title: <?php echo $row_myQuery['titleName']; ?></li>
                          <li>Tel: <?php echo format_phone($row_myQuery['phone']); ?></li>
                          <li>Fax: <?php echo format_phone($row_myQuery['faxNumber']); ?></li>
                          <li><a href="mailto:<?php echo $row_myQuery['email']; ?>">Email: <?php echo $row_myQuery['email']; ?></a></li>
                     <?php }  ?>




                <?php }else if ($row_myQuery['c2Contact'] != ""){ ?>
                 <!-- if contact 2 is not empty display all contacts -->
                <!-- contact 1 information -->  
                 <?php if ($row_myQuery['c1Contact'] != ""){                          
                 $staff_ID = $row_myQuery['c1Contact'];
                 ?>

                <li class="emailGrey"><strong><?php echo $row_myQuery['firstName']; ?> <?php echo $row_myQuery['middleInitial']; ?> <?php echo $row_myQuery['lastName']; ?> <?php echo $row_myQuery['suffix']; ?></strong></li>
                <li>Title: <?php echo $row_myQuery['titleName']; ?></li>
                  <li>Tel: <?php echo format_phone($row_myQuery['phone']);?></li>
                  <li>Fax: <?php echo format_phone($row_myQuery['faxNumber']); ?></li>
                  <li><a href="mailto:<?php echo $row_myQuery['email']; ?>">Email: <?php echo $row_myQuery['email']; ?></a></li>
                  <br />
                   <?php }
               ?>

              <?php if ($row_myQuery['c2Contact'] != ""){                         
                 $staff_ID = $row_myQuery['c2Contact'];
                 $row_myQuery = mysql_fetch_assoc($myQuery);?>

                <li class="emailGrey"><strong><?php echo $row_myQuery['firstName']; ?> <?php echo $row_myQuery['middleInitial']; ?> <?php echo $row_myQuery['lastName']; ?> <?php echo $row_myQuery['suffix']; ?></strong></li>
                <li>Title: <?php echo $row_myQuery['titleName']; ?></li>
                  <li>Tel: <?php echo format_phone($row_myQuery['phone']);?></li>
                  <li>Fax: <?php echo format_phone($row_myQuery['faxNumber']); ?></li>
                  <li><a href="mailto:<?php echo $row_myQuery['email']; ?>">Email: <?php echo $row_myQuery['email']; ?></a></li>
                  <br />
                   <?php }
               ?>

               <?php if ($row_myQuery['c3Contact'] != ""){  

                 $staff_ID = $row_myQuery['c3Contact'];
                 $row_myQuery = mysql_fetch_assoc($myQuery);
                 ?>

                  <!-- partner information -->  
                  <li class="emailGrey"><strong><?php echo $row_myQuery['firstName']; ?> <?php echo $row_myQuery['middleInitial']; ?> <?php echo $row_myQuery['lastName']; ?> <?php echo $row_myQuery['suffix']; ?></strong></li>
                  <li>Title: <?php echo $row_myQuery['titleName']; ?></li>
                  <li>Tel: <?php echo format_phone($row_myQuery['phone']); ?></li>
                  <li>Fax: <?php echo format_phone($row_myQuery['faxNumber']); ?></li>
                  <li><a href="mailto:<?php echo $row_myQuery['email']; ?>">Email: <?php echo $row_myQuery['email']; ?></a></li>

               <?php }
               ?>       




                <?php } else { ?>
                <!-- if only partner display only partner --> 
               <?php if ($row_myQuery['c3Contact'] != ""){  

                    $staff_ID = $row_myQuery['c3Contact'];
                 ?>

                  <!-- partner information -->  
                  <li class="emailGrey"><strong><?php echo $row_myQuery['firstName']; ?> <?php echo $row_myQuery['middleInitial']; ?> <?php echo $row_myQuery['lastName']; ?> <?php echo $row_myQuery['suffix']; ?></strong></li>
                  <li>Title: <?php echo $row_myQuery['titleName']; ?></li>
                  <li>Tel: <?php echo format_phone($row_myQuery['phone']); ?></li>
                  <li>Fax: <?php echo format_phone($row_myQuery['faxNumber']); ?></li>
                  <li><a href="mailto:<?php echo $row_myQuery['email']; ?>">Email: <?php echo $row_myQuery['email']; ?></a></li>

               <?php }
      } 
     } while ($row_myQuery = mysql_fetch_assoc($myQuery)); ?>
4

2 回答 2

5

这是一个基本的INNER JOIN

SELECT
  titles_tbl.titleID, 
  titles_tbl.titleName
FROM
  titles_tbl 
  JOIN staff_tbl ON titles_tbl.titleID = staff_tbl.titleID 
WHERE
  staff_tbl.titleID = 'some value from your long query'

也可以通过WHERE子句中的子查询来完成:

SELECT
  titles_tbl.titleID, 
  titles_tbl.titleName
FROM
  titles_tbl
WHERE
  titleID = (SELECT titleID FROM staff_tbl WHERE <whatever from your long query>)

或者,如果第一个查询旨在返回多行而不是仅返回一行,则需要使用IN()子查询

/* Replace the WHERE clause above with */
WHERE
  titleID IN(SELECT titleID FROM staff_tbl WHERE <whatever from your long query>)
于 2012-09-26T03:00:49.480 回答
0

好的所以我想出了我自己的问题。我创建了更多表格并将它们用作访问点以获取标题和位置信息。谢谢你的帮助 :)

对于其他尝试完成此操作的人来说,下面是我使用的 sql 语句。

    SELECT 
    staff_tbl.staffID, staff_tbl.firstName, staff_tbl.middleInitial, 
    staff_tbl.lastName, staff_tbl.suffix, staff_tbl.accredations, staff_tbl.email,
    staff_tbl.phone, staff_tbl.mobilePhone, staff_tbl.officePhone, staff_tbl.faxNumber,
    staff_tbl.address1, staff_tbl.address2, staff_tbl.cityID, staff_tbl.stateID, 
    staff_tbl.zipCode, location_tbl.locationID, 
    location_tbl.staffID, officelocations_tbl.locationID, 
    officelocations_tbl.officeName, staff_title_tbl.title_ID, staff_title_tbl.staff_ID,
     titles_tbl.titleID, titles_tbl.titleName, city_tbl.cityID, city_tbl.cityName, 
    state_tbl.stateID, state_tbl.state_abreviation, state_tbl.state_name 

    FROM staff_tbl 

    INNER JOIN location_tbl ON location_tbl.staffID = staff_tbl.staffID 
    INNER JOIN officelocations_tbl ON location_tbl.locationID = officelocations_tbl.locationID
    INNER JOIN staff_title_tbl ON staff_title_tbl.staff_ID = staff_tbl.staffID 
    INNER JOIN titles_tbl ON staff_title_tbl.title_ID = titles_tbl.titleID 
    INNER JOIN city_tbl ON city_tbl.cityID = staff_tbl.cityID 
    INNER JOIN state_tbl ON state_tbl.stateID = staff_tbl.stateID 

如您所见,我创建的两个额外表是 location_tbl 和 staff_title_tbl。每一个都有 2 列,一列用于 locationID(或 titleID),一列用于 staffID。使用这些我能够获得所需的所有信息。这只是一个小问题,我还没有完全解决这个问题,如果工作人员与多个位置相关联,它将多次显示该工作人员的完全相同的信息只是位置变了。但至少我已经解决了大部分问题。

再次感谢你的帮助。

于 2012-10-06T20:53:06.153 回答