我有 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
(
cityID
int(11) NOT NULL auto_increment,
cityName
varchar(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
(
locationID
int(11) NOT NULL auto_increment,
officeName
char(255) NOT NULL,
address1
varchar(285) NOT NULL,
address2
varchar(285) 默认 NULL,
cityID
int(11) NOT NULL,
stateID
int(11) NOT NULL,
zipCode
int(11)默认 NULL,
officePhone
varchar(13) 默认 NULL,
contact1
int(11) 默认 NULL,
contact2
int(11) 默认 NULL,
partner
int(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
(
staffID
int(11) NOT NULL auto_increment,
staffID_C2
int(11) NOT NULL,
staffID_P
int(11) NOT NULL,
firstName
varchar(255) NOT NULL,
middleInitial
varchar(15) 默认 NULL,
lastName
varchar(255) NOT NULL,
suffix
varchar(15)默认 NULL,
accredations
varchar(150) 默认 NULL,
email
varchar(255) NOT NULL,
phone
varchar(25) NOT NULL,
mobilePhone
varchar(25) 默认 NULL,
officePhone
varchar(25) 默认 NULL,
faxNumber
varchar(25) 默认 NULL,
address1
varchar(255) NOT NULL,
address2
varchar(255) 默认 NULL,
cityID
int(11) NOT NULL,
stateID
int(11) NOT NULL,
zipCode
int(11) NOT NULL,
titleID
int(11) NOT NULL,
locationID
int(11) NOT NULL,
photoURL
varchar(255) 默认 NULL,
vCardURL
varchar(255) 默认 NULL,
qRCodeURL
varchar(255) 默认 NULL,
resumeURL
varchar(255) 默认 NULL,
biography
longtext,
dateCreated
date NOT NULL, PRIMARY KEY ( staffID
), KEY State
( stateID
), KEY Location
( locationID
), KEY Title
USING BTREE ( titleID
), KEY City
USING BTREE ( cityID
), KEY Staff
USING 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
(
stateID
int(11) NOT NULL auto_increment,
state_abreviation
char(10) NOT NULL,
state_name
char(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
(
titleID
int(11) NOT NULL auto_increment,
titleName
varchar(255) NOT NULL, PRIMARY KEY ( titleID
)) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
--titles_tbl 的记录
INSERT INTO titles_tbl
VALUES ('1', 'Associate Consultant\r\n'); INSERT INTO titles_tbl
VALUES(“2”、“首席财务官”);插入titles_tbl
值('3','顾问');INSERT INTO titles_tbl
VALUES ('4', 'Director, Business Development'); 插入titles_tbl
值('5','保留标题空白');INSERT INTO titles_tbl
VALUES('6','国家信息技术总监');INSERT INTO titles_tbl
VALUES('7','国家运营总监');INSERT INTO titles_tbl
VALUES('8','国家技术总监');插入titles_tbl
值('9','合作伙伴');INSERT INTO titles_tbl
VALUES ('10', '区域顾问'); 插入titles_tbl
值(' 11', '高级顾问'); titles_tbl
INSERT 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)); ?>