How to compare two fields (stock and minimum) of the same Table in (Mysql) using C#?

Asked

Viewed 36 times

-1

Thank you from now on,

Using Visual Studio and Mysql: I need to display in my GRID a list of products that only where the 'stock' field is smaller than the 'minimum' field of my Products Table'.

I can already put the direct value in the line (ex.: value 15) cmd.Parameters.Addwithvalue("@stock", 15);

see q declare the value 15. But I have a field where when registering the ITEM I determine what will be the minimum value.

is an excerpt from the code:

//my grid is listed like this:

private void List() {

            con.AbrirConexao();
        
            sql = "SELECT pro.id, pro.cod, pro.nome, pro.descricao, pro.estoque, forn.nome, pro.entrada, pro.total_compra, pro.valor_compra, pro.valor_venda, pro.data, pro.imagem, pro.fornecedor, pro.minimo, pro.nota  FROM produtos as pro INNER JOIN fornecedores as forn  ON pro.fornecedor = forn.id WHERE **estoque < @estoque** ORDER BY pro.nome asc"; 

            cmd = new MySqlCommand(sql, con.con);
            cmd.Parameters.AddWithValue("**@estoque", 15**);
            MySqlDataAdapter da = new MySqlDataAdapter();
            da.SelectCommand = cmd;
            DataTable dt = new DataTable();
            da.Fill(dt);
            grid.DataSource = dt;
            con.FecharConexao();
            FormatarGD();
        }

//How do I stop instead of putting the fixed value 15 in the code, search the 'minimum' field of the same table 'products' ?

  • Just pass a variable in place of the number :D

1 answer

-1


Make a new select using the mysql MIN() function.

Create a new parameter and run like this:

sqlPrecoMinimo = "SELECT MIN(preco) FROM produtos";

And put him in place of 15, example:

cmd.Parameters.AddWithValue("**@estoque", sqlPrecoMinimo**);
  • Dude, that’s not an answer to my colleague’s question. It doesn’t even need parameter, the comparison value is in the query itself which is the minimum, this would solve here: SELECT pro.id, pro.cod, pro.nome, pro.descricao, pro.estoque, forn.nome, pro.entrada, pro.total_compra, pro.valor_compra, pro.valor_venda, pro.data, pro.imagem, pro.fornecedor, pro.minimo, pro.nota FROM produtos as pro INNER JOIN fornecedores as forn ON pro.fornecedor = forn.id WHERE estoque < pro.minimo ORDER BY pro.nome asc

  • 1

    @Robertodecampos , I used the option 'WHERE stock < pro.minimo' and it worked super well!!!! show helped me a lot, my thanks for the attention.... I’ll still try the other colleagues' options for study purpose.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.