[英]Excessive use of Inner Join for more than 3 tables
Good Day, 美好的一天,
I have 4 tables on my DB (not the actual name but almost similar) which are the ff: employee,education,employment_history,referrence
employee_id is the name of the foreign key from employee table. 我的数据库上有4个表(不是实际名称,但几乎相似),它们是ff:
employee,education,employment_history,referrence
employee_id是employee表中外键的名称。
Here's the example (not actual) data: 这是示例(非实际)数据:
**Employee**
ID Name Birthday Gender Email
1 John Smith 08-15-2014 Male johnsmith@extension.com
2 Jane Doe 00-00-0000 Female janedoe@extension.com
3 John Doe 00-00-0000 Male johndoe@extension.com
**Education**
Employee_ID Primary Secondary Vocation
1 Westside School Westshore H.S SouthernBay College
2 Eastside School Eastshore H.S NorthernBay College
3 Northern School SouthernShore H.S WesternBay College
**Employment_History**
Employee_ID WorkOne StartDate Enddate
1 StarBean Cafe 12-31-2012 01-01-2013
2 Coffebucks Cafe 11-01-2012 11-02-2012
3 Latte Cafe 01-02-2013 04-05-2013
Referrence
Employee_ID ReferrenceOne Address Contact
1 Abraham Lincoln Lincoln Memorial 0000000000
2 Frankie N. Stein Thunder St. 0000000000
3 Peter D. Pan Neverland Ave. 0000000000
NOTE: I've only included few columns though the rest are part of the query. 注意:尽管其余部分是查询的一部分,但我只包括了几列。
And below are the codes I've been working on for 3 consecutive days: 以下是我连续3天从事的代码:
$sql=mysql_query("SELECT emp.id,emp.name,emp.birthday,emp.pob,emp.gender,emp.civil,emp.email,emp.contact,emp.address,emp.paddress,emp.citizenship,educ.employee_id,educ.elementary,educ.egrad,educ.highschool,educ.hgrad,educ.vocational,educ.vgrad,ems.employee_id,ems.workOne,ems.estartDate,ems.eendDate,ems.workTwo,ems.wstartDate,ems.wendDate,ems.workThree,ems.hstartDate,ems.hendDate
FROM employee AS emp INNER JOIN education AS educ ON educ.employee_id='emp.id' INNER JOIN employment_history AS ems ON ems.employee_id='emp.id' INNER JOIN referrence AS ref ON ref.employee_id='emp.id'
WHERE emp.id='$id'");
Is it okay to use INNER JOIN
this way? 这样使用
INNER JOIN
可以吗? Or should I modify my query to get the results that I wanted? 还是应该修改查询以获取所需的结果? I've also tried to use
LEFT JOIN
but still it doesn't return anything .I didn't know where did I go wrong. 我也尝试过使用
LEFT JOIN
但它仍然不返回任何内容。我不知道我在哪里出错。 You see, as I have thought, I've been using the INNER JOIN
in correct manner, (since it was placed before the WHILE CLAUSE
). 正如我所想,您看到的是,我一直在以正确的方式使用
INNER JOIN
(因为它已放置在WHILE CLAUSE
之前)。 So I couldn't think of what could've possible went wrong. 因此,我无法想到可能发生了什么错误。
Do you guys have a suggestion? 你们有建议吗? Thanks in advance.
提前致谢。
I suggest you inspect your use of string literals, such as 'emp.id'
in the join predicates. 我建议您检查一下字符串常量的使用,例如连接谓词中的
'emp.id'
。
I suspect you're not intending to compare to a string literal, but what you want is to compare to an expression that references a column in another table. 我怀疑您不是要与字符串文字进行比较,而是要与引用另一个表中的列的表达式进行比较。
The predicate in your query compares the employee_id
column to a string literal, a constant that contains six characters. 查询中的谓词将
employee_id
列与字符串文字(包含六个字符的常量)进行比较。 It's the single quotes enclosing emp.id
that makes MySQL see that as a literal here: 这是用
emp.id
括起来的单引号,使MySQL在这里将其视为原义:
ON educ.employee_id = 'emp.id'
^ ^
I suspect you want to compare the employee_id
column to the id
column from the employees
table,. 我怀疑您想将
employee_id
列与employees
表中的id
列进行比较。 The absence of single quotes makes MySQL see emp
and id
as identifiers here: 缺少单引号使MySQL在这里将
emp
和id
视为标识符:
ON educ.employee_id = emp.id
^^^ ^^
If you need to "escape" identifiers in MySQL, use the backtick character, not a single quote. 如果需要在MySQL中“转义”标识符,请使用反引号字符,而不要使用单引号。 And enclose each identifier separately, for example:
并分别封装每个标识符,例如:
ON `educ`.`employee_id` = `emp`.`id`
^ ^ ^ ^ ^ ^ ^ ^
I have another suggestion too. 我也有另一个建议。 I suggest you consider using the mysqli or PDO interface instead of the deprecated mysql interface.
我建议您考虑使用mysqli或PDO接口而不是已弃用的mysql接口。
I also suggest you consider possible SQL Injection vulnerabilities, including "unsafe" values as part of the SQL text. 我还建议您考虑可能的SQL注入漏洞,包括“不安全”值作为SQL文本的一部分。 For example, consider what this string evaluates to:
例如,考虑以下字符串的计算结果:
"... emp.id='$id'";
when $id
variable contains a value such as 1' OR '1'='1
当
$id
变量包含诸如1' OR '1'='1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.