简体   繁体   English

以下T-SQL查询有什么问题

[英]What is wrong with the following T-SQL query

I need to write a query against an EMP table that returns the EMPNO, ENAME and JOB values for Employees with the jobs SALESMAN and MANAGER using a Subquery in the WHERE clause. 我需要针对一个EMP表编写一个查询,该查询使用WHERE子句中的子查询返回具有SALESMAN和MANAGER作业的雇员的EMPNO,ENAME和JOB值。

I have this, but am getting the error "too many values". 我有这个,但出现错误“值太多”。 I'm not sure where the extra values are. 我不确定额外的值在哪里。 Any help I can get with this, would be appreciated. 我可以从中获得任何帮助,将不胜感激。

SELECT *
FROM SCOTT.emp
WHERE SCOTT.emp.job=(SELECT SCOTT.emp.empno, SCOTT.emp.ename, SCOTT.emp.job
                     FROM SCOTT.emp 
                     WHERE SCOTT.emp.job="SALESMAN" OR SCOTT.emp.job="MANAGER");
SELECT *
FROM SCOTT.emp
WHERE SCOTT.emp.job in(SELECT  SCOTT.emp.job
                 FROM SCOTT.emp 
                 WHERE SCOTT.emp.job="SALESMAN" OR SCOTT.emp.job="MANAGER");

You are using = with a subquery. 您正在将=与子查询一起使用。 The subquery can return only one column -- SQL Server doesn't support tuples for such comparisons. 子查询只能返回一列-SQL Server不支持元组进行此类比较。

Doesn't this do what you want? 这不是您想要的吗?

SELECT e.*
FROM SCOTT.emp e
WHERE e.job IN ('SALESMAN', 'MANAGER');

There is no need for a subquery in the WHERE clause. WHERE子句中不需要子查询。 It is simply not necessary. 根本没有必要。

There are two options for it: 有两种选择:

1.you can replace '=' with 'IN' as '='(equal to) only expect one value but in your case there are more than one value. 1.您可以将“ =”替换为“ IN”,因为“ =”(等于)仅期望一个值,但在您的情况下,存在多个值。

so your query should be: 因此您的查询应为:

SELECT *
FROM SCOTT.emp
WHERE SCOTT.emp.job IN (SELECT SCOTT.emp.empno, SCOTT.emp.ename, SCOTT.emp.job
                     FROM SCOTT.emp 
                     WHERE SCOTT.emp.job="SALESMAN" OR SCOTT.emp.job="MANAGER");

or as mentioned in someone's answer no need to use sub query in your case directly put 或如某人的回答中所述,无需在您的情况下直接使用子查询

SELECT *
FROM SCOTT.emp 
WHERE SCOTT.emp.job in ("SALESMAN","MANAGER");
  1. If you want to use '=' then use it with OR. 如果要使用“ =”,则将其与OR一起使用。

    SELECT * FROM SCOTT.emp WHERE SCOTT.emp.job ="SALESMAN" OR SCOTT.emp.job ="MANAGER"; 从SCOTT.emp那里选择* SCOTT.emp.job =“ SALESMAN”或SCOTT.emp.job =“ MANAGER”;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM