Mysql 在sql中,如果条件为真,如何选择一行;如果条件为假,如何选择所有行?
假设有一张桌子 临时工 col1 | col---- 100摄氏度 456 | c1 131 | c2 -- 假设coll1=100意味着有效的行查询必须只返回行Mysql 在sql中,如果条件为真,如何选择一行;如果条件为假,如何选择所有行?,mysql,sql,sql-server,relational-database,Mysql,Sql,Sql Server,Relational Database,假设有一张桌子 临时工 col1 | col---- 100摄氏度 456 | c1 131 | c2 -- 假设coll1=100意味着有效的行查询必须只返回行 若表中不存在值,则查询必须返回表中的所有行 注意:我们不能使用if-exists或任何过程事物(仅SQL无TSQL) 我们不能在下面使用 if exists( select 1 from temp where col1=100) begin select 1 from temp where col
若表中不存在值,则查询必须返回表中的所有行 注意:我们不能使用if-exists或任何过程事物(仅SQL无TSQL) 我们不能在下面使用
if exists( select 1 from temp where col1=100)
begin
select 1 from temp where col1=100
end
else
begin
select * from temp where col1=100
end
只需使用
不存在
,即可完成此操作:
SELECT *
FROM temp t
WHERE col1 = 100
OR NOT EXISTS (SELECT 1
FROM temp
WHERE col1 = 100);
SELECT *
FROM temp t
WHERE col1 = 100
OR NOT EXISTS (SELECT 1
FROM temp
WHERE col1 = 100);