[英]Linking a table to a second table and pulling data from the first one to the second one multiple times
I have 2 tables 1 for location and 1 for staff. 我有2个表1的位置和1表的工作人员。 In the location table I have 3 fields: contact1, contact2 and partner. 在位置表中,我有3个字段:contact1,contact2和partner。 I need each of these to select a name, email and phone number from the staff table and display it. 我需要每个人都可以从人员表中选择姓名,电子邮件和电话号码并显示出来。 I can only seem to get it to pull one contact at a time. 我似乎只能一次拉动一次联系。 Here is what I have 这是我所拥有的
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
That only displays the office information and the one contact I want it to do something like this 那只显示办公室信息和一个我希望它做这样的联系人
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
doing this however gives me an error 这样做但是给我一个错误
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in ..... 警告:mysql_fetch_assoc():提供的参数不是..中的有效MySQL结果资源。
Is there another way to list them by using the staffID and linking it to the three different fields in another table? 是否有另一种方法可以通过使用staffID并将其链接到另一个表中的三个不同字段来列出它们?
I tried the solution on here How to select data from multiple tables using joins/subquery properly? 我在这里尝试了解决方案如何正确使用联接/子查询从多个表中选择数据? (PHP-MySQL) but it wouldn't recognize the second select statement in the from section. (PHP-MySQL),但无法识别from部分中的第二个select语句。 I also tried the concat and it said it wasn't a valid sql query, it was the same for the inner join. 我也尝试了concat,它说这不是有效的sql查询,对于内部联接来说是相同的。 I'm using php/mysql database. 我正在使用php / mysql数据库。 The message I posted above is what I kept getting when using any of the examples on that page. 我在上面发布的消息是使用该页面上任何示例时得到的信息。 The only thing that changed was the line the error was being thrown on. 唯一改变的是抛出错误的那一行。
I was thinking of just creating 4 separate sql statements. 我正在考虑只创建4个单独的sql语句。 I know there is a way to do this but the ones I've tried don't seem to work. 我知道有办法做到这一点,但我尝试过的似乎无效。
Thank you for any help. 感谢您的任何帮助。
Edited after assistance rendered below 在提供以下帮助后进行编辑
ok So I got it to display but with one minor issue when I tell it to display the contact information for sf1 it is only showing 2 entries when there should be 13. I have a total of 29 locations that I want to be able to display not all locations have contact1 or contact2 but all have a partner. 好的,所以我可以显示它,但是当我告诉它显示sf1的联系信息时出现一个小问题,它应该显示13时仅显示2个条目。我总共希望显示29个位置并非所有位置都有contact1或contact2,但是所有位置都有一个伙伴。 Here is the code I have edited to reflect your suggestions: 这是我为了反映您的建议而编辑的代码:
$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>";
}
I did try to have the if statement say 我确实尝试过if语句说
if ($db_field['c1Firstname'] != "" && $db_field['c2Firstname'] == "")
but it didn't seem to work either. 但它似乎也不起作用。
You are joining staff_tbl multiple times without a suffix, try something like this: 您要多次加入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)
Of course you have to add sf1.[column2] AS c1Column2, sf2.[column2] AS c2Column2, sf3.[column2] AS partnerColumn2 and so on for all the columns you want from the different JOINS
. 当然,您需要为来自不同JOINS
所有列添加sf1。[column2] AS c1Column2,sf2。[column2] AS c2Column2,sf3。[column2] AS partnerColumn2等。
You can see your exact error using echo mysql_error();
您可以使用echo mysql_error();
查看确切的错误echo mysql_error();
right after you run mysql_query()
在您运行mysql_query()
Note to other readers: I have edited my answer based on the question below 其他读者注意事项:我已根据以下问题编辑了答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.