如何将字符串附加到MySQL中的现有字段?

时间:2010-09-22 01:08:45

标签: sql mysql string

我想将我所有记录中的代码更新为他们目前的内容加上_标准的任何想法?

例如,如果代码是apple_1和apple_2,我需要它们是apple_1_standard和apple_2_standard

之前:

id   code
------------
1    apple_1 
1    apple_2

Psuedo查询:

update categories set code = code + "_standard" where id = 1;

预期结果:

id   code
----------------------
1    apple_1_standard 
1    apple_2_standard

2 个答案:

答案 0 :(得分:167)

您需要使用MySQL中的CONCAT()函数进行字符串连接:

UPDATE categories SET code = CONCAT(code, '_standard') WHERE id = 1;

答案 1 :(得分:2)

更新图片字段以添加完整的URL,忽略空字段:

public static class Stack<E> {

    private E[] elem;
    public int top;
    Stack(int s) {
        elem = (E[])new Object[s];
        top = -1;
    }
    public void push(E ch) {
        elem[++top] = ch;
    }
    public E pop() {
        E pom = elem[top];
        top--;
        return pom;
    }
    public boolean isEmpty() {
        if (top == -1) return true;
        else return false;
    }
    public int size() {
        return top + 1;
    }
}

public static String onp_do_inf(String onpForm) {

    Stack<String> stos = new Stack<String>(onpForm.length() * 3);
    String operator1 = null, operator2 = null;
    stos.push("0");

    for (int i = 0; i < onpForm.length(); i++) {
        char x = onpForm.charAt(i);
        if (x != '+' && x != '-' && x != '='
                && x != '*' && x != '/'
                && x != '%' && x != '^' && x != '<' && x != '>' && x != '~') {
            stos.push(Character.toString(onpForm.charAt(i)));


        } else if ((stos.size() - 2) > 0) {
            if (x == '~') {
                operator1 = stos.pop();
                stos.push(x + operator1);

            } else {
                operator1 = stos.pop();
                operator2 = stos.pop();
                if ((stos.top == 0 && x == '=')) {
                    stos.push(operator2 + x + operator1);
                }
                else if ( stos.top == 0){
                    stos.push(operator2 + x + operator1);
                }
                else if ((x == '/') || (x == '*' && stos.top > 1)
                        || (x == '%' && stos.top == 1) || (x == '<')
                        || (x == '>') || (x == '^' && stos.top >= 1) || (x == '=')) {
                    stos.push(operator2 + x + operator1);
                } else {
                    stos.push("(" + operator2 + x + operator1 + ")");
                }
            }
        }
    }
    return stos.pop();
}