免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 8117 | 回复: 6
打印 上一主题 下一主题

mysql中有没有与oracle中decode功能类似的函数? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-09 22:24 |只看该作者 |倒序浏览
mysql中有没有与oracle中decode功能类似的函数?

论坛徽章:
0
2 [报告]
发表于 2008-10-09 22:26 |只看该作者
在mysql下decode函数好像有两种解释。一种是加密,另外一种是比较(我从mysql网站上查到的)。具体解释如下:  
  1、http://www.mysql.com/documentati ... 449261/frameset.htm  
  DECODE(x,y(i),...,z)   
  DECODE(x,y(i),...,z)   is   a   special   function   that   decodes   expressions   in   accordance   with   their   values.  
   
  x  
    check_expression  
    Expression   (   expression)   for   which   a   comparison   is   to   be   carried   out   with   the   values   in   y(i)  
     
  y(i)  
    search_and_result_spec  
    <search_and_result_spec>   ::=   <search_expression>,   <result_expression>   (y(i)=u(i),v(i),   i=1,...)  
   
  Combination   of   the   comparison   value   u(i)   and   the   value   v(i)   that   is   to   replace   this   comparison   value  
     
  z  
    default_expression  
    Optional   default   value  
     
  u(i)  
    search_expression  
    Comparison   value   that   is   to   be   replaced   by   v(i)   if   it   matches   x  
     
  v(i)  
    result_expression  
    Value   that   is   to   be   replace   u(i)  
     
   
  The   data   types   of   x   and   u(i)   must   be   comparable.   The   data   types   of   v(i)   and   z   must   be   comparable.   The   data   types   of   u(i)   and   v(i)   do   not   have   to   be   comparable.  
   
  DECODE   compares   the   values   of   x   with   the   values   u(i)   consecutively.   If   a   match   is   found,   the   result   of   DECODE   is   the   value   v(i)   in   the   combination   y(i)=u(i),v(i).  
   
  A   match   is   present   if   x   and   u(i)   are   NULL   values.   The   comparison   of   the   special   NULL   value   with   any   other   value   never   results   in   a   match.  
   
  If   a   match   is   not   found,   DECODE   supplies   the   result   of   z.   If   z   is   not   specified,   the   NULL   value   is   the   result   of   DECODE.  
   
   
   
  Example   table:   room  
   
  The   room   type   identifiers   are   to   be   replaced   in   the   output   by   an   identifier   declared   in   the   DECODE   function.  
   
  SELECT   hno,   price,   DECODE   (roomtype,   
  'SINGLE',   1,   'DOUBLE',   2,   'SUITE',   3)   room_code  
  FROM   room  
   
  2、http://www.mysql.com/doc/en/Miscellaneous_functions.html  
  DECODE(crypt_str,pass_str)   
  Descrypts   the   encrypted   string   crypt_str   using   pass_str   as   the   password.   crypt_str   should   be   a   string   returned   from   ENCODE().

http://topic.csdn.net/t/20031201/09/2512527.html

http://snipurl.com/47mla

论坛徽章:
0
3 [报告]
发表于 2008-10-09 22:30 |只看该作者
我知道mysql中decode是跟oracle中不一样的,我想问的是在mysql中如何做这种操作。
select s.recognition,b.b_listdate,s.storename,decode(s.state,1,'在库','未在库') 。。。。

论坛徽章:
1
双子座
日期:2013-08-19 14:56:16
4 [报告]
发表于 2008-10-10 09:13 |只看该作者
IF(COALESCE(s.state,0),'存在','不存在')


不知道可以不

论坛徽章:
0
5 [报告]
发表于 2008-10-10 09:44 |只看该作者

回复 #1 xmfail 的帖子

对,mysql中用if(s.state=1,'在库','未在库')
或者用case语法,大部分数据库都支持的,如(case when s.state=1 then '在库' else '未在库' end)

论坛徽章:
0
6 [报告]
发表于 2008-10-10 15:58 |只看该作者
case when 吧。

论坛徽章:
0
7 [报告]
发表于 2008-10-10 16:17 |只看该作者
倒不妨试试 MySQL 的 ENUM 数据类型

  1. mysql> INSERT EnumTest VALUES('在库','是','保密','金卡VIP会员','满意'),('不在库','否','男','匿名用户','非常不满意');
  2. Query OK, 2 rows affected (0.02 sec)
  3. Records: 2  Duplicates: 0  Warnings: 0

  4. mysql> select * from EnumTest;
  5. +--------+----------+------+-------------+------------+
  6. | 状态   | 是否可用 | 性别 | 用户等级    | 用户满意度 |
  7. +--------+----------+------+-------------+------------+
  8. | 在库   | 是       | 保密 | 金卡VIP会员 | 满意       |
  9. | 不在库 | 否       | 男   | 匿名用户    | 非常不满意 |
  10. +--------+----------+------+-------------+------------+
  11. 2 rows in set (0.00 sec)

  12. mysql> show create table EnumTest\G
  13. *************************** 1. row ***************************
  14.        Table: EnumTest
  15. Create Table: CREATE TABLE `EnumTest` (
  16.   `状态` enum('在库','不在库') DEFAULT NULL,
  17.   `是否可用` enum('是','否') DEFAULT NULL,
  18.   `性别` enum('男','女','保密') DEFAULT NULL,
  19.   `用户等级` enum('匿名用户','普通注册用户','银卡VIP会员','金卡VIP会员','钻石VIP会员') DEFAULT NULL,
  20.   `用户满意度` enum('非常不满意','不满意','一般','满意','很满意') DEFAULT NULL
  21. ) ENGINE=MyISAM DEFAULT CHARSET=utf8
  22. 1 row in set (0.00 sec)
复制代码


[ 本帖最后由 lovetide 于 2008-10-10 16:21 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP