如何删除mysql查询中的空逗号 [英] How to remove inbetween empty comma in mysql query

查看:172
本文介绍了如何删除mysql查询中的空逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际数据(,,,演示,xxxx ,,, yyy ,,,)为(demo,xxxx,yyy)



什么我试过了:



i试过了

actual data (,,,demo, ,xxxx,,,yyy,,,) to be (demo,xxxx,yyy)

What I have tried:

i had tried

SELECT TRIM(BOTH ',' FROM ',,,demo, ,xxxx,,,yyy,,,');
SELECT REPLACE(TRIM(TRIM(',' FROM ',,,demo, ,xxxx,,,yyy,,,')), ',,', ',');



不删除任何一个plz帮助之间的空逗号??


its not removing the empty comma in between any one plz help??

推荐答案

需要澄清:查询中的这个是什么
Clarification required: what is this one in the query
',,,demo, ,xxxx,,,yyy,,,'





Mysql并不是真的用于文本操作



如果您使用像php这样的服务器端语言,可以preg_replace删除字符串中的逗号并将该变量发送到select query。



Mysql is not really meant for text manipulation

If you are using a server side language like php, you can preg_replace to remove commas in the string and send that variable to select query.


暗示(你在主题列表中使用.php)我会认为你是属查询。



在这种情况下,当你添加每个值时,如果它是空的(),则在逗号后面插入NULL(没有单引号!)。 br />


或者,您不是直接使用php生成查询,而是将其传递给您的php。在这种情况下,您可以使用str_replace(',,',',NULL,',yourString)修复除最后一个逗号以外的所有逗号;并且,由于NULL是预先允许的,你可以使用Trim()来删除最后一个逗号 - 或者,如果字符串中的最后一个字符是逗号,则在字符串的末尾添加NULL。



上述内容在很大程度上取决于您处理丢失数据的方式:这是使用默认值进入存储过程吗?一个默认值的表?而且,这些逗号是如何实现的?





By implication (that you're using .php in your subject list) I'll presume you're generating the query.

In this case, as you add each value, if it's empty(), then insert NULL (no single quotes!) after the comma.

Alternatively, you're not generating the query directly with php, but it's being passed in to your php. In this case, you can fix all but the last comma with str_replace(',,', ',NULL,', yourString); and, since NULL's are presumbly allowed, you can the use Trim() to get rid of the last comma - or, if the last character in your string is a comma, add NULL to the end of your string.

The above is heavily dependent upon how you handle missing data: is this going to a stored procedure with default values? A table with default values? And also, how did the commas get there in the first place?




这篇关于如何删除mysql查询中的空逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆