如果表中的多个字段可能为空,如何更新?

时间:2012-08-10 05:57:56

标签: java mysql phpmyadmin

我想在名为5的表格中更新用户个人信息的Personnel-table个字段。

用户可能希望更新所有字段,部分字段或根本不更新。

用户可能想要更新的字段包括:FirstNameLastNamePasswordUserNameAddress

该代码执行此操作:

public boolean updateUserInfo(String _idnumber , String _usernameNew , String _passwordNew ,  
        String _addressNew , String _firstNameNew , String _lastNameNew) throws SQLException
{

    ResultSet resultSet = null;

    String sqlStatement = "UPDATE PersonnelTable set `UserName` = ? WHERE `IdNumber` = ?";
    this.m_prepared = (PreparedStatement) this.m_connection.prepareStatement(sqlStatement);
    this.m_prepared.executeUpdate("USE Personnel");

    // set the ID number & account number 


    /**
     *  first 
     */
    m_prepared.setString(1, _usernameNew);
    m_prepared.setString(2, _idnumber);

    // execute first query - update password
    if (_usernameNew!= "")
    {
        resultSet = m_prepared.executeQuery();
        return true;
    }

    /**
     *  Second 
     */

    sqlStatement = "UPDATE PersonnelTable set `FirstName` = ? WHERE `IdNumber` = ?";
    this.m_prepared = (PreparedStatement) this.m_connection.prepareStatement(sqlStatement);
    m_prepared.setString(1, _firstNameNew);
    m_prepared.setString(2, _idnumber);

    if (_firstNameNew != "")
    {
        resultSet = m_prepared.executeQuery();
        return true;
    }


    /**
     *  Third
     */

    sqlStatement = "UPDATE PersonnelTable set `LastName` = ? WHERE `IdNumber` = ?";
    this.m_prepared = (PreparedStatement) this.m_connection.prepareStatement(sqlStatement);
    m_prepared.setString(1, _lastNameNew);
    m_prepared.setString(2, _idnumber);

    if (_lastNameNew!= "")
    {
        resultSet = m_prepared.executeQuery();
        return true;
    }

    /**
     *  Fourth
     */

    sqlStatement = "UPDATE PersonnelTable set `Address` = ? WHERE `IdNumber` = ?";
    this.m_prepared = (PreparedStatement) this.m_connection.prepareStatement(sqlStatement);
    m_prepared.setString(1, _addressNew);
    m_prepared.setString(2, _idnumber);

    if (_addressNew!= "")
    {
        resultSet = m_prepared.executeQuery();
        return true;
    }



    /**
     *  Fifth
     */

    sqlStatement = "UPDATE PersonnelTable set `Password` = ? WHERE `IdNumber` = ?";
    this.m_prepared = (PreparedStatement) this.m_connection.prepareStatement(sqlStatement);
    m_prepared.setString(1, _passwordNew);
    m_prepared.setString(2, _idnumber);

    if (_passwordNew!= "")
    {
        resultSet = m_prepared.executeQuery();
        return true;
    }



    return false;
} 

但正如您所看到的,代码太大了,因为我运行了5次查询。

如何告诉mysql如果每个字段都不是空字符串"",那么请更新 在一个mysql查询中需要的字段?是可能的还是我必须每次都这样做?

此致

1 个答案:

答案 0 :(得分:5)

您可以在单个查询中轻松完成此操作:

UPDATE PersonnelTable
SET FirstName = IF(? <> "", ?, FirstName),
    LastName = IF(? <> "", ?, LastName),
    Password = IF(? <> "", ?, Password),
    UserName = IF(? <> "", ?, UserName),
    Address = IF(? <> "", ?, Address)
WHERE IdNumber = ?;

如果您的字段可以包含""这样的空白值,那么您可以使用IF(? IS NOT NULL, ?, field_name)代替空白。