使用 WHERE 子句的 SQL 帮助。以下代码出现错误

发布于 2024-12-08 20:29:11 字数 596 浏览 1 评论 0原文

大家好,我正在尝试创建一个查询来显示员工姓名和员工编号以及他们的超级名称和超级编号。列表还包括没有任何主管的员工。我为它编写了一些代码,但它给了我错误,因为它不正确。 有谁愿意帮我解决这个问题吗?干杯,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半寸时光 2024-12-15 20:29:11

试试这个:

SELECT employee.ename, employee.empno, boss.ename, boss.empno
FROM emp employee
LEFT JOIN emp boss
ON employee.super = boss.empno 

LEFT JOIN 意味着左表(雇员)中的每一行都将被返回,即使右表(老板)中没有匹配的行。

Try this:

SELECT employee.ename, employee.empno, boss.ename, boss.empno
FROM emp employee
LEFT JOIN emp boss
ON employee.super = boss.empno 

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).

素年丶 2024-12-15 20:29:11

您已经使用了employee.super并且它试图被评估为布尔值:

OR     employee.super AND boss.empno IS NULL; 

您需要将它与某些东西进行比较。

我会用:

SELECT employee.ename, employee.empno, boss.ename, boss.empno 
FROM   emp employee
LEFT JOIN emp boss ON employee.super = boss.empno

You have used employee.super and it is trying to be evaluated as a boolean in:

OR     employee.super AND boss.empno IS NULL; 

You need to be comparing it to something.

I would use:

SELECT employee.ename, employee.empno, boss.ename, boss.empno 
FROM   emp employee
LEFT JOIN emp boss ON employee.super = boss.empno
開玄 2024-12-15 20:29:11

不要使用隐式 SQL '89 连接语法,它是一种反模式。
走出侏罗纪公园并使用显式 SQL '92 连接语法:

SELECT 
  employee.ename
  , employee.empno
  , boss.ename
  , boss.empno 
FROM   emp employee
INNER JOIN emp boss on (employee.super <=> boss.empno)

您可以使用 <=> 运算符:

null = null   -> null  -> no join
1 = 1         -> true  -> join
1 = 0         -> false -> no join

null <=> null -> true  -> join
1 <=> 1       -> true  -> join
1 <=> 0       -> false -> no join

<=> 是 MySQL 特定的,除了您将使用的 MySQL:
(e.super = b.empno or (e.super is null and b.empno is null))

请注意,上面的连接与左连接不同join:

//code from Mark Byers' answer
SELECT employee.ename, employee.empno, boss.ename, boss.empno 
FROM emp employee 
LEFT JOIN emp boss 
ON employee.super = boss.empno  

内连接产生更少行。

免责声明:以下链接是一个快速而肮脏的连接可视化,请对此持保留态度。但它确实形象化了总体想法。
请参阅: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:

SELECT 
  employee.ename
  , employee.empno
  , boss.ename
  , boss.empno 
FROM   emp employee
INNER JOIN emp boss on (employee.super <=> boss.empno)

You can use the <=> operator:

null = null   -> null  -> no join
1 = 1         -> true  -> join
1 = 0         -> false -> no join

null <=> null -> true  -> join
1 <=> 1       -> true  -> join
1 <=> 0       -> false -> no join

<=> 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:

//code from Mark Byers' answer
SELECT employee.ename, employee.empno, boss.ename, boss.empno 
FROM emp employee 
LEFT JOIN emp boss 
ON employee.super = boss.empno  

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

手心的温暖 2024-12-15 20:29:11

使用:

SELECT employee.ename, employee.empno, boss.ename, boss.empno, CASE WHEN boss.empno IS NULL THEN 'no supervisor found'
                                                                    ELSE 'supervisor found'
                                                                END
  FROM employee LEFT JOIN emp boss ON boss.empno = employee.super

Use:

SELECT employee.ename, employee.empno, boss.ename, boss.empno, CASE WHEN boss.empno IS NULL THEN 'no supervisor found'
                                                                    ELSE 'supervisor found'
                                                                END
  FROM employee LEFT JOIN emp boss ON boss.empno = employee.super
宫墨修音 2024-12-15 20:29:11
SELECT employee.ename, employee.empno, boss.ename, boss.empno
FROM   emp employee, emp boss
WHERE  employee.super = boss.empno
UNION
SELECT employee.ename, employee.empno, '(no boss)', ('no boss')
FROM   emp employee
WHERE  employee.super IS NULL;
SELECT employee.ename, employee.empno, boss.ename, boss.empno
FROM   emp employee, emp boss
WHERE  employee.super = boss.empno
UNION
SELECT employee.ename, employee.empno, '(no boss)', ('no boss')
FROM   emp employee
WHERE  employee.super IS NULL;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文