GROUP BY issue when SELECT to dataGridView from 2 tables

前端 未结 1 1393
独厮守ぢ
独厮守ぢ 2021-01-23 04:17

With little help of you I made this two QUERY. I posted pictures so you can see that in green squares are empty values which I would like to see and in

相关标签:
1条回答
  • 2021-01-23 05:09

    If I'm understanding what you're after, this is the part that's keeping you from getting the rows with no entries in klisluz:

    WHERE zajsluz.akce= '{0}' and klisluz.subkey ='" + vyberradek + "'
    

    For the rows that don't exist in klisluz, subkey will be NULL, which won't match anything with that WHERE clause. To get these rows as well, you can replace your current WHERE clause with:

    WHERE zajsluz.akce= '{0}' and ISNULL(klisluz.subkey, '" + vyberradek + "') ='" + vyberradek + "'
    

    Here's the full line of code:

    string sQuery = string.Format("SELECT zajsluz.akce,zajsluz.text,klisluz.pocet,klisluz.subkey,zajsluz.ID FROM zajsluz LEFT JOIN klisluz ON zajsluz.ID=klisluz.IDzajsluz WHERE zajsluz.akce= '{0}' and ISNULL(klisluz.subkey, '" + vyberradek + "') = '" + vyberradek + "' GROUP BY klisluz.subkey,zajsluz.akce,zajsluz.text,klisluz.pocet,zajsluz.ID", sZakce);
    

    To determine whether the checkbox should be checked:

    if (precti3.HasRows)
    {
        precti3.Read();
        if (precti3.Item("subkey") != Null)
        {
            row.Cells[5].Value = true;
        }
        else
        {
            row.Cells[5].Value = false;
        }
    }
    

    I think that's how it'd go in C#. I work in VB.NET, used an online converter for this.

    0 讨论(0)
提交回复
热议问题