I have a simple query in my code (shown below) written by my colleague. What does t mean here? Also what is the role of the ; inside the query? I am dead sure that t is not any table, nor any field anywhere in my database. And guess what this query works!!
string query = @"SELECT COUNT(*) FROM (SELECT AttemptNo FROM attempt_exercise
WHERE FK_UId = @uId AND FK_EId = @eId AND Mode = @mode)t;
";
The code follows like this (for any other info if required):
MySqlCommand cmd = new MySqlCommand(query, _conn);
cmd.Parameters.AddWithValue("@uId", uId);
cmd.Parameters.AddWithValue("@eId", eId);
cmd.Parameters.AddWithValue("@mode", mode);
attemptCount = Convert.ToInt32(cmd.ExecuteScalar());
_conn.Close();
return attemptCount;
Your colleague created a query (
SELECT COUNT(*)) with a subquery that he namedt. Thistis just a temporary table name which refers toHe could have feasibly named it
tempto be a bit more explicit. The reason that this becomes like a table is because, in MySQL, aSELECTquery returns rows of data which act like a table. So, this inner query gets theAttemptNo, and creates a temporary tablet. The outer query then counts this data.The
;inside the query is to make it a full statement when the string query is called by the program. If this weren’t included, the Stringquerywouldn’t contain a valid MySQL statement. The final;is to complete the assignment for the variable.