Mysql命令substring列的值由count计算

时间:2014-03-08 11:41:44

标签: php mysql codeigniter

我想通过子串的计数来命令我的查询。

表:

id shop_update

1  a,b,c
2  a,b
3  c
4  a,'',c

我只是按计数来订购

id  shop_update count
 1   a,b,c       3
 2   a,b         2
 3   c           1
 4   a,'',c      2

我的查询是

$this->db->select("*,LENGTH(shop_update) - LENGTH(REPLACE(shop_update,',','')) AS counts");
$this->db->order_by("counts","DESC");
$this->db->from('at_shop');

但它只在计数

中返回0
 id  shop_update count
     1   a,b,c       0
     2   a,b         0
     3   c           0
     4   a,'',c      0

我的问题是LENGTH(REPLACE(REPLACE(shop_update,',',''),'''',''))它的运行空间如LENGTH(REPLACE(REPLACE(shop_update,',(spaces are added in the running query but actual code the spaces are not there) ',''),'''',''))

请看差异 有什么帮助吗?

2 个答案:

答案 0 :(得分:0)

问题出在这里

LENGTH(REPLACE(shop_update,' , ',''))

在你的表中,数据为a,b,b等,没有空格,LENGTH与原始字符串相同,因为没有什么可以替换和行

LENGTH(shop_update) - LENGTH(REPLACE(shop_update,',' , ''))

始终返回0.即

length of string - LENGTH(REPLACE(shop_update,',' , '')) 

所以用作

LENGTH(REPLACE(shop_update,',',''))

答案 1 :(得分:0)

select `id`, `name`,
LENGTH(REPLACE(REPLACE(shop_update,',',''),'''',''))
From Table1;

<强> Fiddle