search

Home  >  Q&A  >  body text

Error: MySQL - operand should contain 1 column.

<p>This query works when I type it directly into MySQL, but not in C#. </p> <pre class="brush:php;toolbar:false;">double deltaTime = (DateTime.Now - collectTime).TotalSeconds; string query = String.Format("UPDATE buildings LEFT JOIN server_buildings ON buildings.global_id = server_buildings.global_id AND buildings.level = server_buildings.level SET buildings.storage = buildings.storage (server_buildings.speed * {0} * IF(buildings. boost >= NOW(), 2, 1)) WHERE buildings.global_id IN('generator', 'test')", deltaTime / 3600d);</pre> <p>Just got the error MySQL - operand should contain 1 column. Thanks for the suggestion! :)</p><p>It is expected to increase in value in "storage" over time. </p><p><br /></p>
P粉529245050P粉529245050499 days ago470

reply all(1)I'll reply

  • P粉710454910

    P粉7104549102023-07-25 11:53:11

    I'm relatively new to MySQL. This is a query with parameters.

    string query = String.Format("UPDATE buildings JOIN server_buildings ON buildings.global_id = server_buildings.global_id AND buildings.level = server_buildings.level SET buildings.storage = buildings.storage + (server_buildings.speed * ?Num * IF(buildings.boost >= NOW(), 2, 1)) WHERE buildings.global_id IN('generator', 'test')");
                         
                using (MySqlCommand command = new MySqlCommand(query, connection))
                {
                    MySqlParameter param = new MySqlParameter("?Num", (deltaTime / 3600d));
                    command.Parameters.Add(param);
          
                   command.ExecuteNonQuery();
                }

    reply
    0
  • Cancelreply