I have a query to fetch two values:
string query = @"SELECT price, weight
FROM map
WHERE width = @width AND height = @height LIMIT 1";
if (_connSource.State != ConnectionState.Open)
_connSource.Open();
MySqlCommand cmd = new MySqlCommand(query, _connSource);
cmd.Parameters.AddWithValue("width", width);
cmd.Parameters.AddWithValue("height", height);
r = cmd.ExecuteReader();
if (!r.Read())
{
r.Close();
query = @"SELECT retail_price, 0
FROM globe
WHERE PK_Id = @PK_Id LIMIT 1"
cmd = new MySqlCommand(query, _connSource);
cmd.Parameters.AddWithValue("PK_Id ", 1);
r = cmd.ExecuteReader();
}
What I need is to get price
and weight
according to a condition, but if it is not present in the table, then I need to get another two fields retail_price
, and a constant 0 (doesnt matter what it is) from a totally new table with no constraints from the previous table. Can I get the two in a single query?
Note: Kindly give me optimized queries which doesn't force reading the same values more than once (this function gets executed thousands of times in one single operation, so speed is very critical - a reason why I'm trying to get it in one query). Thanks..