我的查询有问题,它告诉我错误
ORA-00932: inconsistent datatypes: expected NCHAR got NUMBER
00932.00000 - "inconsistent datatypes: expected% s got% s"
* Cause:
* Action:
Error in line: 54, column: 43
子查询返回学生性别的字母“f”或“m”,其中有更多,这可行,但问题是我如何将该字母分配给变量 Where genero_a =
SELECT NOMBRE, COALESCE(DIRECCION,CORREO, 10) comm
FROM ESTUDIANTE
WHERE GENERO_A =
(SELECT GENERO_A
FROM ESTUDIANTE
GROUP BY GENERO_A
HAVING COUNT(GENERO_A) =
(SELECT MAX(COUNT(*)) as CONTAR
FROM ESTUDIANTE
GROUP BY GENERO_A))
ORDER BY NOMBRE;
我完全不熟悉sql,感谢您的理解。
- TABLA ESTUDIANTE
CREATE TABLE ESTUDIANTE(
ID_LECTOR VARCHAR2(50) NOT NULL,
NOMBRE VARCHAR2(50),
APELLIDO VARCHAR2(50),
GENERO_A CHAR(1) CONSTRAINT CH_GENERO_A CHECK(GENERO_A IN ('f', 'm')),
DIRECCION NVARCHAR2(100),
CORREO NVARCHAR2(50)
);
ALTER TABLE ESTUDIANTE ADD CONSTRAINT PK_ESTUDIANTE PRIMARY KEY(ID_LECTOR);
答案 0 :(得分:4)
错误发生在COALESCE(DIRECCION,CORREO, 10)
,因为所有字段都没有相同的数据类型。
<强>解决方案:强>
您必须将NUMBER
10转换为与DIRECTION和CORREO类似的数据类型。
只需将上述代码更改为COALESCE(DIRECCION,CORREO, CAST(10 as NVARCHAR2(2)))
。
CAST函数将值从一种数据类型转换为另一种数据类型。
更新了查询:
SELECT NOMBRE, COALESCE(DIRECCION,CORREO, CAST(10 as NVARCHAR2(2))) comm
FROM ESTUDIANTE
WHERE GENERO_A =
(SELECT GENERO_A
FROM ESTUDIANTE
GROUP BY GENERO_A
HAVING COUNT(GENERO_A) =
(SELECT MAX(COUNT(*)) as CONTAR
FROM ESTUDIANTE
GROUP BY GENERO_A))
ORDER BY NOMBRE;
或者,从您提供的link,
SELECT NOMBRE, COALESCE(DIRECCION,CORREO, N'10') comm
FROM ESTUDIANTE
WHERE GENERO_A =
(SELECT GENERO_A
FROM ESTUDIANTE
GROUP BY GENERO_A
HAVING COUNT(GENERO_A) =
(SELECT MAX(COUNT(*)) as CONTAR
FROM ESTUDIANTE
GROUP BY GENERO_A))
ORDER BY NOMBRE;
或者,
SELECT NOMBRE, COALESCE(DIRECCION,CORREO, N''||10) comm
FROM ESTUDIANTE
WHERE GENERO_A =
(SELECT GENERO_A
FROM ESTUDIANTE
GROUP BY GENERO_A
HAVING COUNT(GENERO_A) =
(SELECT MAX(COUNT(*)) as CONTAR
FROM ESTUDIANTE
GROUP BY GENERO_A))
ORDER BY NOMBRE;