我的主要问题是我不知道如何说 select 1 on this cond select 2 on that cond。所以我让他们都发表了自己的声明。有没有办法将它们结合起来?
select 1 from FlagContent where user=? and rev = ?;
select 2 from ContentRevision r
join ContentRevision r2 on r.content=r2.content
join FlagContent fc on fc.user=? and fc.rev = r2.id
where r.id=?;
最佳答案
使用UNION
“合并”相似的结果集。
SQL UNION allows you to combine two or more than two result set from multiple tables together.
但是,使用它有一些条件:
- 每个 SELECT 语句中的列数必须相同
- SELECT 语句的列列表中的列的数据类型必须相同或至少可转换。
select 1 from FlagContent where user=? and rev = ?;
UNION ALL
select 2 from ContentRevision r
join ContentRevision r2 on r.content=r2.content
join FlagContent fc on fc.user=? and fc.rev = r2.id
where r.id=?;
关于sql - 将两个 select <value> 合并到一个语句中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4760123/