Return NULL instead of repeated value in certain columns

帅比萌擦擦* 提交于 2020-01-06 20:24:43

问题


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:

What it returns now

What I want it to return:

SQL Query:

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) 

回答1:


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.

You use it like this:

$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>';



回答2:


USE variables to show only the first row.

The default value for CASE is null so i didnt include the ELSE

This assume there isnt negative saldo so starting value is -1

SELECT CASE WHEN rn = 1 THEN Sld_dev_ctbl END as Sld_dev_ctbl,
       CASE WHEN rn = 1 THEN Saldo END as Saldo           
       Vlr_actual,
FROM ( 
        SELECT *,
               (@rownum := if( @saldo = Saldo,
                               @rownum + 1, -- increase the counter
                               if (@saldo := Saldo,
                                   0,  -- reset the counter for next block
                                   0
                                  )
                             )
               ) as rn
        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) 
        CROSS JOIN (select @rownum := 0, @saldo := -1) params
        ORDER BY --<provide some field to choose which one will be first>
) T


来源:https://stackoverflow.com/questions/34751841/return-null-instead-of-repeated-value-in-certain-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!