我有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变量?
编辑: 这是数据库结构和一些值
SET FOREIGN_KEY_CHECKS = 0;
- city_tbl
DROP TABLE IF EXISTS city_tbl
;
CREATE TABLE city_tbl
(
cityID
int(11)NOT NULL auto_increment,
cityName
varchar(255)NOT NULL,
主要关键(cityID
)
)ENGINE = MyISAM AUTO_INCREMENT = 83 DEFAULT CHARSET = utf8;
- city_tbl的记录
INSERT INTO city_tbl
VALUES(' 1',' Aloha');
插入city_tbl
VALUES(' 2',' Ann Arbor');
- officelocations_tbl
DROP TABLE IF EXISTS 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
)
)ENGINE = MyISAM AUTO_INCREMENT = 31 DEFAULT CHARSET = utf8;
- officelocations_tbl的记录
INSERT INTO officelocations_tbl
VALUES(' 1',' Atlanta',' 5555 Some Ave','#311' ,' 3',' 10',' 30041','',null,null,' 26') ;
插入officelocations_tbl
VALUES(' 2',' Austin',' 5555 Some Ave','',&#39 ; 4',' 43',' 78734',' 555-555-1212',' 72',null,&# 39; 81&#39);
- staff_tbl
DROP TABLE IF EXISTS 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
借词
dateCreated
日期非空,
PRIMARY KEY(staffID
),
KEY State
(stateID
),
KEY Location
(locationID
),
KEY Title
使用BTREE(titleID
),
KEY City
使用BTREE(cityID
),
KEY Staff
使用BTREE(staffID
)
)ENGINE = MyISAM AUTO_INCREMENT = 102 DEFAULT CHARSET = utf8;
- staff_tbl的记录
INSERT INTO staff_tbl
VALUES(' 1',' 1',' 1',' John',&# 39;',' Doe','','',' johndoe@someemail.com',&#39 ; 5555551212','',' 5555551212',' 5555551212',' 5555 Some Ave。',' 8号套房,#317',' 21',' 42',' 55555',' 3',' 0& #39;,'','','','',' 1',& #39; 2012-08-02&#39);
插入staff_tbl
VALUES(' 2',' 2',' 2',' Jane','& #39;,' Doe','','',' jandoe@someemail.com',' 5555551212&# 39;,'',' 5555551212',' 5555551212',' 555555 SW Some Ave','' ,' 1',' 37',' 55555',' 3',' 0',' ','','','',' 2',' 2012-08-02& #39);
- state_tbl
DROP TABLE IF EXISTS 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,
主要关键(stateID
)
)ENGINE = MyISAM AUTO_INCREMENT = 54 DEFAULT CHARSET = utf8;
- state_tbl的记录
插入state_tbl
价值观(' 51',' DC','华盛顿特区');
插入state_tbl
VALUES(' 1',' AL',' Alabama');
- titles_tbl
DROP TABLE IF EXISTS titles_tbl
;
CREATE TABLE titles_tbl
(
titleID
int(11)NOT NULL auto_increment,
titleName
varchar(255)NOT NULL,
主要关键(titleID
)
)ENGINE = MyISAM AUTO_INCREMENT = 15 DEFAULT CHARSET = utf8;
- titles_tbl的记录
INSERT INTO titles_tbl
VALUES(' 1',' Associate Consultant \ r \ n&n;');
插入titles_tbl
VALUES(' 2','首席财务官');
插入titles_tbl
VALUES(' 3','顾问');
插入titles_tbl
VALUES(' 4','导演,业务发展');
插入titles_tbl
VALUES(' 5',' LEAVE TITLE BLANK');
插入titles_tbl
VALUES(' 6','国家信息技术总监');
插入titles_tbl
VALUES(' 7','国家运营总监');
插入titles_tbl
VALUES(' 8','国家技术总监');
插入titles_tbl
个值(' 9','合作伙伴');
插入titles_tbl
VALUES(' 10','区域顾问');
插入titles_tbl
VALUES(' 11','高级顾问');
插入titles_tbl
VALUES(' 12','副总统');
插入titles_tbl
个值(' 13','项目会计师');
插入titles_tbl
VALUES(' 14','');
示例输出如下:
它的工作除外,因为它输出的所有标题对于每个工作人员都是相同的。我使用的代码是我之前为第二个select语句发布的代码。我不知道如果我能弄清楚如何将titles_tbl加入主sql语句会更容易。我已经尝试了从Union到第一个中的第二个选择的几个变体,它们似乎都不起作用或者它们抛出错误。
这是我一直使用的第一个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个触点,它会切换contact1和contact2,而在我只有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和partner。如果联系人2为空,则仅显示联系人1和伙伴,而如果联系人2存在,则将显示所有3个联系人,以及如果没有联系人1或联系人2则仅显示伙伴的其他语句。通过它循环的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)); ?>
答案 0 :(得分: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>)
答案 1 :(得分: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。使用这些我能够获得所需的所有信息。这是一个小问题,就像一个旁注,我没有完全解决问题,如果工作人员与多个地点相关联,它将多次显示该工作人员的完全相同的信息。只有位置发生了变化。但至少我已经完成了大部分工作。
再次感谢您的帮助。