如果row的值为null,如何更改返回值

时间:2014-05-06 11:05:58

标签: mysql sql select

我在DA类(Java)中有一个SQL语句。方法getStagescore用于获取特定实习的所有分数(方法是荷兰语)。我意识到有些分数是null

public ArrayList<Stagescore> getStagescore(int stageId) {
    ArrayList<Stagescore> stagescores = new ArrayList<Stagescore>();
    PreparedStatement statement = null;
    ResultSet resultSet = null;

    try {
        statement = connection.prepareStatement("SELECT * FROM stagescore INNER JOIN score ON stagescore.waarde = score.waarde AND stagescore.criteriumId = score.criteriumId inner join criterium ON score.criteriumId = criterium.criteriumId WHERE stagescore.stageId = ?");
        statement.setInt(1, stageId);
        resultSet = statement.executeQuery();
        while (resultSet.next()) {
            Stagescore stagescore = new Stagescore();
            stagescore.setStagescoreId(resultSet.getInt("stagescore.stagescoreId"));
            stagescore.setStageId(resultSet.getInt("stagescore.stageId"));
            stagescore.setCriteriumId(resultSet.getInt("stagescore.criteriumId"));

            stagescore.setValue(resultSet.getInt("stagescore.value"));


            Score score = new Score();
            score.setCriteriumId(resultSet.getInt("score.criteriumId"));
            score.setScorebetekenis(resultSet.getString("score.scorebetekenis"));
            score.setValue(resultSet.getInt("score.value"));

            Criterium criterium = new Criterium();
            criterium.setCriteriumId(resultSet.getInt("criterium.criteriumId"));
            criterium.setCriterium(resultSet.getString("criterium.criterium"));

            score.setCriterium(criterium);
            stagescore.setScore(score);

            stagescores.add(stagescore);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            statement.close();
            if (resultSet != null) {
                resultSet.close();
            }
        } catch (SQLException e) {
        }
    }

问号是针对给定的外部价值。有时stagescore.value的值为空...如果值为空,我如何将值从stagescore.value更改为0?

3 个答案:

答案 0 :(得分:1)

您可以使用coalesce()(ANSI标准函数):

SELECT *
FROM stagescore inner join
     score
     on coalesce(stagescore.value, 0) = score.value and
        stagescore.criteriumId = score.criteriumId inner join
     criterium
     on score.criteriumId = criterium.criteriumId
where stagescore.stageId = ?

答案 1 :(得分:0)

SELECT * FROM stagescore 
   inner join score on IFNULL(stagescore.value,0) = score.value 
    and stagescore.criteriumId = score.criteriumId 
   inner join criterium on score.criteriumId = criterium.criteriumId 
where stagescore.stageId = ?

答案 2 :(得分:0)

在执行此Mysql命令之前,检查变量是否包含值,否则在此变量上存储0

if(!$variable)
$variable = 0;