是否有 SQL 语法可以根据同一 table 中第 3 列的相等值在 2 列中搜索来创建新列?
Is there a SQL syntax that will create new column by searching in 2 columns based on equal value of 3rd column in same table?
我有 1 个 table,字段名为:class_code、name1、class_code1、name2、class_code2。我想搜索 class_code 的值,并将其显示为 class_code 和仅名称,其中 class_code = class_code1 或 class_code = class_code2。结果将显示 class_code 和 name
是否可以使用 sql 连接语法?
这是table:
class_code name1 class_code1 name2 class_code2
C0001 John C0002 Ben C0001
C0002 Ren C0001 Elizh C0002
C0001 Jeff C0001 Harry C0001
我需要的输出是:
class_code name
C0001 Ben
C0002 Elizh
C0001 Jeff
C0001 Harry
如果意图是 class_code1 和 name1 应该放在一起,class_code2 和 name2 应该放在一起,您可以使用 UNION
:
SELECT class_code1 AS class_code, name1 AS name FROM table
UNION
SELECT class_code2 AS class_code, name2 AS name FROM table
如果我做对了
SELECT class_code, name1
FROM tbl
WHERE class_code = class_code1
UNION --ALL
SELECT class_code, name2
FROM tbl
WHERE class_code = class_code2
我有 1 个 table,字段名为:class_code、name1、class_code1、name2、class_code2。我想搜索 class_code 的值,并将其显示为 class_code 和仅名称,其中 class_code = class_code1 或 class_code = class_code2。结果将显示 class_code 和 name
是否可以使用 sql 连接语法?
这是table:
class_code name1 class_code1 name2 class_code2
C0001 John C0002 Ben C0001
C0002 Ren C0001 Elizh C0002
C0001 Jeff C0001 Harry C0001
我需要的输出是:
class_code name
C0001 Ben
C0002 Elizh
C0001 Jeff
C0001 Harry
如果意图是 class_code1 和 name1 应该放在一起,class_code2 和 name2 应该放在一起,您可以使用 UNION
:
SELECT class_code1 AS class_code, name1 AS name FROM table
UNION
SELECT class_code2 AS class_code, name2 AS name FROM table
如果我做对了
SELECT class_code, name1
FROM tbl
WHERE class_code = class_code1
UNION --ALL
SELECT class_code, name2
FROM tbl
WHERE class_code = class_code2