在SQL查询上添加“三元”列

时间:2018-10-27 00:01:08

标签: sql tsql

这些是我的表格:

create table Utilizador(
    ID int IDENTITY(1,1),
    email varchar(50) NOT NULL UNIQUE,
    name varchar(100),
    institution varchar(100),
);

create table Author(
    ID int,
    PRIMARY KEY(ID),
    FOREIGN KEY(ID) REFERENCES Utilizador(ID),
);

--Tables Evaluator and President are the same as Author

我想要这样的视图:

CREATE VIEW GlobalViewOfUser AS
    SELECT U.ID, 
    U.email, 
    isAuthor = (EXISTS SELECT * FROM Author WHERE ID = U.ID) ? 1 : 0,
    isEvaluator = (EXISTS SELECT * FROM Evaluator WHERE ID = U.ID) ? 1 : 0,
    isPresident = (EXISTS SELECT * FROM President WHERE ID = U.ID) ? 1 : 0,
    (...)
    FROM User U, Author A, Evaluator E, President P 
    WHERE U.ID = A.ID AND U.ID = R.ID AND U.ID = P.ID

我不知道这样的事情是否可能...

2 个答案:

答案 0 :(得分:0)

这是我想出的:

%module test
%include <std_except.i>
%inline %{
class CustomException : public std::exception
{
};
%}

不知道我怎么花了几个小时。.>>>

答案 1 :(得分:-1)

如果您希望那些isXXX列起作用,那么大多数SQL方言都可以使用的一件事是(例如isAuthor):

SELECT 
...
U.email,
CASE WHEN A.ID = U.ID THEN 1 ELSE 0 END AS isAuthor
...
FROM User U, Author A ...
;

希望这会有所帮助。