[英]How to get the value from the primary key AND foreign key when joining mySQL tables?
I have two tables, employee
as the parent and license
as the child. 我有两个表,
employee
为父母, license
为孩子。 They both have a Lic_ID
column for reference, this column is the PK
in license
and the FK
in employee
. 他们都有一个
Lic_ID
列供参考,该列是license
的PK
和employee
的FK
。 The license
table also has a column Lic_Type
which holds the name of the license. license
表还具有一列Lic_Type
,其中包含许可证名称。
I am trying to create a table with list boxes so the employee
table can be updated. 我正在尝试创建带有列表框的表,以便可以更新
employee
表。 The list box value
needs to be populated with the license.Lic_ID
and the license.Lic_Type
is to be displayed in the option
. 该列表框中
value
需要与填充license.Lic_ID
和license.Lic_Type
是要显示在option
。 Here is what I have: 这是我所拥有的:
(Employee name, Id, etc. called out up here)
<?php
echo "<select name=\"Lic\">";
echo "<option value=\"\">Select...</option>";
$sql = $mysqli->query("SELECT Lic_ID, Lic_Type FROM license");
while($row = $result->fetch_assoc())
{
echo "<option value=\"" . $row['Lic_ID'] . "\">" . $row['Lic_Type'] . "</option>";
}
echo "</select>";
?>
So that works good, it shows the license type and has the value set to the license id. 这样可以很好地工作,它会显示许可证类型并将其值设置为许可证ID。 What I want to do is have
<option selected="selected">
if the license id is set for an employee. 我要执行的操作是
<option selected="selected">
如果已为员工设置了许可证ID)。 This code doesn't work, but I think it illustrates what I'm trying to do: 这段代码不起作用,但是我认为它说明了我正在尝试做的事情:
<?php
echo "<select name=\"Lic\">";
echo "<option value=\"\">Select...</option>";
$sql = $mysqli->query("SELECT license.Lic_ID, license.Lic_Type, employee.Lic_ID FROM employee INNER JOIN license ON employee.Lic_ID = license.Lic_ID");
while($row = $result->fetch_assoc())
{
echo "<option value=\"" . $row['license.Lic_ID'] . "\"";
if($row['employee.Lic_ID'] = $row['license.Lic_ID']){echo "selected=\"selected\";}
echo ">" . $row['license.Lic_Type'] . "</option>";
}
echo "</select>";
?>
Is there a way to accomplish what I'm trying to do? 有什么方法可以完成我想做的事情?
I think there may have been some confusion on what exactly I was trying to accomplish, I apologize for not being very clear. 我想我可能要完成的工作可能有些困惑,对于不太清楚,我深表歉意。 Anyways, I stumbled over the answer today, so I thought I should post it.
无论如何,我今天偶然发现了答案,所以我认为应该发布它。
$sql1 = ("SELECT Emp_Name, Lic_MAT_ID FROM employee");
if(!$result_employee_query = $mysqli->query($sql1))
{
die ("There was an error getting the records from the employee table");
}
while($employee = $result_employee_query->fetch_assoc())
{
echo "Employee Name: " . $employee['Emp_Name'] . "<br>";
echo "License: ";
echo "<select>";
$sql2 = ("SELECT Lic_MAT_ID, Lic_MAT_Type FROM license_mat");
if(!$result_license_query = $mysqli->query($sql2))
{
die ("There was an error getting the records from the license table");
}
while($license = $result_license_query->fetch_assoc())
{
echo "<option value=\"" . $license ['Lic_MAT_ID'] . "\"";
if($license['Lic_MAT_ID'] == $employee['Lic_MAT_ID'])
{
echo " selected=\"selected\"";
}
echo ">" . $license ['Lic_MAT_Type'] . "</option>";
}
echo "</select><br>";
}
As I understand your problem: You want to see if the License has been added to ANY users or has it been unassigned. 据我了解,您的问题是:您想查看许可证是否已添加到任何用户或是否尚未分配。 If any of the users have the license set then it's "selected", othervize not.
如果有任何用户设置了许可证,则将其“选中”,否则不进行设置。
First you have to assign the keyword "multiple"
to your select
object to make it a listbox: 首先,您必须为
select
对象分配关键字"multiple"
以使其成为列表框:
echo "<select name=\"Lic\" multiple=\"multiple\">";
Second: I would write this kind of query: 第二:我会写这种查询:
$sql = $mysqli->query("SELECT l.Lic_ID, l.Lic_Type, e.cnt FROM licence l left outer join (select Lic_id, count(*) cnt from employee group by Lic_id) e on l.Lic_ID=e.Lic_id");
It selects the Lic_ID
, Lic_Type
and the count
of how many employees have the respective Lic_ID set to it ( left outer join
) 它选择的
Lic_ID
, Lic_Type
和count
的多少员工有各自的Lic_ID设置为它( left outer join
)
and in the code just check, if the count
is higher then 0 并在代码中检查,如果
count
大于0
if($row['cnt'] > 0){
echo "selected=\"selected\";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.