简体   繁体   中英

How to update multiple fields in a table if one them might be blank?

I want to update 5 fields of user personal information in a table called Personnel-table .

The user might want to update all the fields , part of them , or nothing at all .

The fields that the user might want to update are : FirstName , LastName , Password , UserName and Address .

That code does that :

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;
} 

But as you can see , the code is too big , since I run the query 5 times .

How can I tell mysql that if each of the fields is not the empty string "" , then update the needed field , in one mysql-query ? is that possible or I must do that separately every time ?

Regards

You can easily do this in a single query as:

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

If your fields can have blank values like "" then you can use IF(? IS NOT NULL, ?, field_name) instead of blank.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM