我有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个位置我想成为能够显示并非所有位置都有contact1或contact2,但都有合作伙伴。以下是我编辑的代码以反映您的建议:
$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'] == "")
但它似乎也没有用。
答案 0 :(得分: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)
当然你必须添加sf1。[column2] AS c1Column2,sf2。[column2] AS c2Column2,sf3。[column2] AS partnerColumn2,依此类推,以便从不同的JOINS
获取所有列。
运行echo mysql_error();
mysql_query()
查看确切的错误
其他读者请注意:我根据以下问题编辑了我的答案