mysql和sqlserver中substring(order_date,int,int)的区别

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhupengqq/article/details/89502234

mysql和sqlserver中substring(order_date,int,int)的区别

select a.*,b.goods_name,b.goods_shichangjia*nums totalfee from (select sum(goods_quantity) nums,goods_id from t_orderitem where order_id in 
(select  order_id from t_order  where substring(order_date,1,7)='2019-03' ) group by goods_id ) 
a left join t_goods b on a.goods_id = b.goods_id  and b.goods_del='no'  

同样的这句话,在sqlserver中可以正常显示

但是在mysql中

却查到是空,我很好奇,也很疑惑,难道哪里写错了,级联错了吗,还是join   on那里,不可能啊,不可能有错啊

于是我把每句话拆分来看

select sum(goods_quantity) nums,goods_id from t_orderitem   结果是7    2

这句话没有错,正常,但是select  order_id from t_order  where substring(order_date,0,8)='2019-03'  查到为空

这就是不对了,不可能的,那么这句话错在哪里了呢?我发现,出现substring(order_date,int,int)上

原来在sqlsever中截取字段从0开始,mysql从1开始

好了,可以正常显示了

SUBSTRING函数从特定位置开始的字符串返回一个给定长度的子字符串。 MySQL提供了各种形式的子串功能。

我们将在以下部分中检查SUBSTRING函数的每种形式。

 
  1. SUBSTRING(string,position);

  2. SUBSTRING(string FROM position);

有两个参数:

  • string参数是要提取子字符串的字符串。
  • position参数是一个整数,用于指定子串的起始字符,position可以是正或负整数。

如果position为正,则SUBSTRING函数从字符串的开始处提取子字符串。请参阅以下字符串。

例如,要从“MySQL SUBSTRING”字符串中获取子字符串:“SUBSTRING”,子串的位置必须从7开始,如以下SELECT语句:

 
  1. mysql> SELECT SUBSTRING('MYSQL SUBSTRING', 7);

  2. +---------------------------------+

  3. | SUBSTRING('MYSQL SUBSTRING', 7) |

  4. +---------------------------------+

  5. | SUBSTRING |

  6. +---------------------------------+

  7. 1 row in set

请注意,如果position参数为零,则SUBSTRING函数返回一个空字符串:

 
  1. mysql> SELECT SUBSTRING('MYSQL SUBSTRING', 0);

  2. +---------------------------------+

  3. | SUBSTRING('MYSQL SUBSTRING', 0) |

  4. +---------------------------------+

  5. | |

  6. +---------------------------------+

  7. 1 row in set

除了特定于MySQL的语法之外,可以使用SQL标准语法与FROM关键字一起调用SUBSTRING函数。

例如,以下语句使用SQL标准语法从"MySQL SUBSTRING"字符串中获取"SUBSTRING"

 
  1. mysql> SELECT SUBSTRING('MySQL SUBSTRING' FROM -10);

  2. +---------------------------------------+

  3. | SUBSTRING('MySQL SUBSTRING' FROM -10) |

  4. +---------------------------------------+

  5. | SUBSTRING |

  6. +---------------------------------------+

  7. 1 row in set

MySQL SUBSTRING具有位置和长度

如果要指定要从字符串中提取的子字符串的长度,可以使用以下形式的SUBSTRING函数:

SUBSTRING(string,position,length);

以下是上述语句的SQL标准版本,它更长,但更具表现力。

SUBSTRING(string FROM position FOR length);

除了stringposition参数之外,SUBSTRING函数还有一个额外的length参数。length是一个正整数,用于指定子字符串的字符数。

如果positionlength的总和大于字符串的字符数,则SUBSTRING函数将返回一个从位置开始到字符串末尾的子串。

猜你喜欢

转载自blog.csdn.net/zhupengqq/article/details/89502234