[英]mysql add “prefix” to every column
I need to add a 'prefix' in front of every value in a certain column. 我需要在某列中的每个值前面添加一个“前缀”。
Example: all fields in column x are: 200, 201, 202, 203, etc. I need them to be pn_200, pn_201, pn_202, pn_203, etc. 示例:x列中的所有字段均为:200,201,202,203等。我需要它们为pn_200,pn_201,pn_202,pn_203等。
Is there a way to use ALTER
or MODIFY
commands to do this? 有没有办法使用
ALTER
或MODIFY
命令来执行此操作?
I would like something like ADD to BEGINNING of * column_name 'pn_'
我
ADD to BEGINNING of * column_name 'pn_'
类的东西
Or perhaps a way to do it in PHP ? 或者也许是用PHP做到这一点的方法? Maybe get the value of the field, turn that into a variable, and do something like.
也许得到该字段的值,将其转换为变量,并执行类似的操作。
`$variablex = `'SELECT column_name FROM table'
$result = mysqli_query($con, variablex);
foreach($r=mysqli_fetch_row($result) {
`ADD TO BEGINNING OF * column_name 'pn_'`
Is there anyway to do that? 反正有吗?
Actually it's even easier. 实际上它更容易。
UPDATE table SET column_name = CONCAT('pn_', column_name)
Without a WHERE clause it will update all the rows of your table 如果没有WHERE子句,它将更新表的所有行
SELECT concat('pn_', column_name) AS column_name
FROM yourtable
but why do this at the database layer? 但为什么在数据库层呢? It's trivial to do it in PHP:
在PHP中执行它是微不足道的:
SELECT column_name ...
while($row = mysql_fetch_assoc($result)) {
$data = 'pn_' . $row['column_name'];
}
i think this is what you want 我想这就是你想要的
$que = "SELECT column_name FROM table";
$res = mysql_query($que, $con);
if(mysql_num_rows($res)>0){
while($row = mysql_fetch_array($res)){
echo "PN_". $row['column_name'];
}
}
if you only want to show it wit pn_ at the beginnning but if you want to change it also in the database you need to select all get the id value and update it with concatination 如果你只想在开始时用pn_显示它,但如果你想在数据库中也改变它,你需要选择all获取id值并用concatination更新它
UPDATE MyTable
SET MyField = CONCAT('pn_', MyField)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.