一、[基本查询语句展示优化]
#根据type查询 SELECT id,title,type FROM table WHERE type=1; SELECT id,title,type FROM table WHERE type=2;
用if优化
#if(expr,true,false) SELECT id,title,type,if(type=1,1,0) as type1,if(type=2,1,0) as type2 FROM table; SELECT id,title,type,if(type=1,1,0) as type1,if(type=2,1,0) as type2 FROM table;
用case when优化
#case...when...then...when...then...else...end SELECT id,title,type,case type WHEN 1 THEN 'type1' WHEN 2 THEN 'type2' ELSE 'type error' END as newType FROM table;
二、[统计数据性能优化]
#两次查询不同条件下的数量 SELECT count(id) AS size FROM table WHERE type=1 SELECT count(id) AS size FROM table WHERE type=2
用if优化
#sum方法 SELECT sum(if(type=1, 1, 0)) as type1, sum(if(type=2, 1, 0)) as type2 FROM table #count方法 SELECT count(if(type=1, 1, NULL)) as type1, count(if(type=2, 1, NULL)) as type2 FROM table #亲测二者的时间差不多 #建议用sum,因为一不注意,count就会统计了if的false中的0
用case when优化
#sum SELECT sum(case type WHEN 1 THEN 1 ELSE 0 END) as type1, sum(case type WHEN 2 THEN 1 ELSE 0 END) as type2 FROM table #count SELECT count(case type WHEN 1 THEN 1 ELSE NULL END) as type1, count(case type WHEN 2 THEN 1 ELSE NULL END) as type2 FROM table
亲测查询两次和优化后查询一次的时间一样,优化时间为1/2
相关推荐
详细分析 mysql 中的 控制流程语句 if 以及 case when 的使用, 里面 的 sql 语句,已经在 本机上亲自实践,
详细讲解了case、when的用法.ASE表达式可以在SQL中实现if-then-else型的逻辑,而不必使用PL/SQL。CASE的工作方式与DECODE()类似,但应该使用CASE,因为它与ANSI兼容。
我们在进行项目开发中,经常会遇到多条件模糊...最近写数据库存储过程的时候经常使用case when 语句,正好可以用这个语句解决一下以上问题。以SQL中的NorthWind数据库为例,我要操作的是其中的Employees表,该表中默认
1.Case函数的用法 (1)使用类似:switch-case与if-else if。 (2)语法: case [字段] when 表达式 then 显示数据 when 表达式 then 显示数据 else 显示数据 end (3)百分制转换素质教育 1)如图:我们要将显示的数据...
Sql_Case_When 很有用的SQL用法!
本文实例讲述了mysql存储过程之case语句用法。分享给大家供大家参考,具体如下: 除了if语句,mysql提供了一个替代的条件语句CASE。...我们可以使用简单CASE语句来检查表达式的值与一组唯一值的匹配,上述sql中,case_e
Mysql的if既可以作为表达式用,也可在存储过程中作为流程控制语句使用,如下是做为表达式使用: IF表达式 代码如下: IF(expr1,expr2,expr3) ...作为表达式的if也可以用CASE when来实现: 代码如下: se
case_value是要被判断的值或表达式,接下来是一系列的WHEN-THEN块,每一块的when_value参数指定要与case_value比较的值,如果为真,就执行statement_list中的SQL语句。如果前面的每一个块都不匹配就会执行ELSE块指定...
今天群里有人问了个问题是这样的: 然后有群友是这样回答的 代码如下: select name,sum(case when stype=4 then money*(-1) else money end ) as M from table group by name 我想了想,应该可以用IF函数 于是改了下...
2010-04-11 12:42 642 if-else,case-when使用.sql 2010-04-11 12:33 147 临时表的创建使用.sql 2010-04-11 12:06 293 函数返回table.sql 2009-08-31 12:30 664 函数返回变量以及table.sql 2009-09-16 17:34 182 创建...
SQL CASE 表达式是一种通用的条件表达式,类似于其它语言中的 if/else 语句,下面这篇文章主要给大家介绍了关于case when语句的报错问题的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
Although 2 GB of address space may seem like a large amount of memory, application such as SQL Server could leverage more memory if it were available. The boot.ini option /3GB was created for those ...
--语 句 功 能 --数据操作 SELECT --从数据库表中检索数据行和列 INSERT --向数据库表添加新数据行 DELETE --从数据库表中删除数据行 UPDATE --更新数据库表中... when job_level = ’1’ then e_wage*1.08
If you build a clustered index without specifying the unique keyword, SQL Server forces uniqueness by adding a uniqueifier to the rows when necessary. This uniqueifier is a 4-byte value added as an ...
MyBatis 提供了强大的动态 SQL 功能,它允许你根据参数的值来动态地构建 SQL ..., <when>, <otherwise>:类似于 Java 中的 switch-case-default 结构。 xml <select id="findUsersByType" resultType="User"> SEL
select @m=case when @m between 1 and 3 then 1 when @m between 4 and 6 then 4 when @m between 7 and 9 then 7 else 10 end select @time=datename(year,@date)+'-'+convert(varchar(10),@m)+'-01' ...
SQL数据库:select case when len(field)>10 then left(field,10)+'...' else field end as news_name,news_id from tablename Access数据库:SELECT iif(len(field)>2,left(field,2)+'...',field) FROM ...
我们知道查询状态为cancel的订单,SQL语句可以这样写:SELECT o.oid,o.moneyreceipt,o.moneyget,o.thecurrency,o.status FROM qorder o WHERE o.status = ‘cancel’ SQL语句能查询出正确的数据,但是当我们想查询...
select type,sum(case vender when 'A' then pcs else 0 end),sum(case vender when 'C' then pcs else 0 end),sum(case vender when 'B' then pcs else 0 end) FROM tablename group by type 显示结果: type ...