我有两个 SQL 表(不能修改,这是一个如何在没有类似列的情况下获取它的问题),我需要在使用内部连接列出城市和州时找出表类型:
CREATE TABLE authors
(
au_id CHAR(3) NOT NULL,
au_fname VARCHAR(15) NOT NULL,
au_lname VARCHAR(15) NOT NULL,
phone VARCHAR(12) ,
address VARCHAR(20) ,
city VARCHAR(15) ,
state CHAR(2) ,
zip CHAR(5) ,
CONSTRAINT pk_authors PRIMARY KEY (au_id)
);
CREATE TABLE publishers
(
pub_id CHAR(3) NOT NULL,
pub_name VARCHAR(20) NOT NULL,
city VARCHAR(15) NOT NULL,
state CHAR(2) ,
country VARCHAR(15) NOT NULL,
CONSTRAINT pk_publishers PRIMARY KEY (pub_id)
);
我正在尝试使用 CASE 来正确输出选择:
SELECT CASE
WHEN LCASE(SUBSTR(/*suppose id*/, 0, 1)) == 'a'
THEN 'author'
ELSE 'publisher'
END
AS type, city, state
FROM authors
INNER JOIN publishers
这些表的格式使得 id 不同,但名称也不同:
INSERT INTO authors VALUES('A01','Sarah','Buchman','718-496-7223',
'75 West 205 St','Bronx','NY','10468');
INSERT INTO publishers VALUES('P01','Abatis Publishers','New York','NY','USA');
我试图达到这样的输出:
type | city | state
--------------------------------------------------
author | Rochester | NY
author | Syracuse | NY
publisher | New York | NY
当两个表没有相似的列时,如何有效地比较它们?我可以调用表上的字段值来检查它是否为空而不抛出 SQL 异常吗?