在某些列中返回NULL而不是重复的值 [英] Return NULL instead of repeated value in certain columns

查看:136
本文介绍了在某些列中返回NULL而不是重复的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的查询必须在某些列中允许重复值,但是我希望它在出现重复值时在查询中返回空值.

My query must allow repeated values in certain columns, but I want it to return a null in the query when a repeated value occurs.

以下两个图像显示了我的问题:

The following two images show my problem:

现在返回的内容

我希望它返回的内容:

SQL查询:

SELECT *
FROM completo_sem_saldo AS a
LEFT JOIN posicao_contabil AS b ON (a.Cliente = b.Cliente_Posicao)
LEFT JOIN saldo_analitico AS c ON (a.Cliente = c.Cliente_Saldo_Analitico)
LEFT JOIN titulos_em_ser AS d ON (a.Cliente = d.Cliente_Titulos_Em_Ser) 

推荐答案

您可以在PHP中使用一个函数进行此操作,该函数执行重复检查并在重复时返回":

You could do this in PHP with a function that does the repetition check and returns "" when it is a repetition:

function blankIfRepeat($result, $key, &$map) {
    if (isset($map[$key]) && $map[$key] == $result[$key]) {
        return ""; // it is the same value we encountered last time
    }
    $map[$key] = $result[$key];
    return $result[$key];
}

在最后一个参数之前注意&,以确保调用者获得对该参数所做的更改.

Note the & before the last parameter to make sure the caller gets the changes made to that argument.

您可以这样使用它:

$map = array(); // initialise the variable used to track repetitions
while($resul1 = mysqli_fetch_assoc($query1)) {
    $nome = $resul1['Nome'];
    // call the function for the fields that need blanks for repeated values:
    $sld_dev_ctbl = blankIfRepeat($resul1, 'Sld_dev_ctbl', $map);
    $saldo = blankIfRepeat($resul1, 'Saldo', $map);

    $vlr_atual = $resul1['Vlr_atual'];

    $tabela .= '<tr>';
    $tabela .= '<td>'.$nome.'</td>';
    $tabela .= '<td>'.$sld_dev_ctbl.'</td>';
    $tabela .= '<td>'.$saldo.'</td>';
    $tabela .= '<td>'.$vlr_atual.'</td>';
    $tabela .= '</tr>';
}
$tabela .= '</table>';

这篇关于在某些列中返回NULL而不是重复的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆