使用 WHERE 子句的 SQL 帮助。以下代码出现错误
大家好,我正在尝试创建一个查询来显示员工姓名和员工编号以及他们的超级名称和超级编号。列表还包括没有任何主管的员工。我为它编写了一些代码,但它给了我错误,因为它不正确。 有谁愿意帮我解决这个问题吗?干杯,
SELECT employee.ename, employee.empno, boss.ename, boss.empno
FROM emp employee, emp boss
WHERE employee.super = boss.empno
OR employee.super AND boss.empno IS NULL;
提前感谢
-Jay
好吧,我找到了答案,并且使用了外连接运算符。左连接有效,但我们的课程中没有教授这一点。谢谢你。使用外连接的答案如下:
SELECT employee.ename, employee.empno, boss.ename, boss.empno
FROM emp employee, emp boss
WHERE employee.super = boss.empno(+);
所以它仍然会返回employee.super 为空的行。
Hi guys I am trying to create a query to display employee name and employee number along with their super’s name and super’s number. Listing also includes employees who don’t have any supervisor. I wrote a bit of code for it and it gives me and error because its not right.
Would anyone be kind enough to give me a hand with this. Cheers guys
SELECT employee.ename, employee.empno, boss.ename, boss.empno
FROM emp employee, emp boss
WHERE employee.super = boss.empno
OR employee.super AND boss.empno IS NULL;
Thanks in advance
-Jay
Allright guys I found the answer and I used outer join operator. The left join works but we are not taught that in our course. Thanks for that. The answer using the outer join is as follows:
SELECT employee.ename, employee.empno, boss.ename, boss.empno
FROM emp employee, emp boss
WHERE employee.super = boss.empno(+);
So it will still return rows that has null for employee.super.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个:
LEFT JOIN
意味着左表(雇员)中的每一行都将被返回,即使右表(老板)中没有匹配的行。Try this:
The
LEFT JOIN
means that every row in the left table (employee) will be returned, even if there is no matching row in the right table (boss).您已经使用了
employee.super
并且它试图被评估为布尔值:您需要将它与某些东西进行比较。
我会用:
You have used
employee.super
and it is trying to be evaluated as a boolean in:You need to be comparing it to something.
I would use:
不要使用隐式 SQL '89 连接语法,它是一种反模式。
走出侏罗纪公园并使用显式 SQL '92 连接语法:
您可以使用
<=> 运算符:
<=>
是 MySQL 特定的,除了您将使用的 MySQL:(e.super = b.empno or (e.super is null and b.empno is null))
请注意,上面的连接与左连接不同join:
内连接产生更少行。
免责声明:以下链接是一个快速而肮脏的连接可视化,请对此持保留态度。但它确实形象化了总体想法。
请参阅:http://www.codinghorror .com/blog/2007/10/a-visual-explanation-of-sql-joins.html
Do not use implicit SQL '89 join syntax, it's an anti-pattern.
Get out of jurassic park and use explicit SQL '92 join syntax:
You can use the
<=>
operator:<=>
is MySQL specific, outside of MySQL you'd use:(e.super = b.empno or (e.super is null and b.empno is null))
Note that above join is not the same as a left join:
The inner join produces fewer rows.
Disclaimer: The following link is a quick and dirty visualisation of joins, to be taken with a grain of salt. But it does visualize the general idea.
See: http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
使用:
Use: