什么是“t”在这个SQL中参考? [英] What does "t" refer to in this SQL?

查看:124
本文介绍了什么是“t”在这个SQL中参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中有一个简单的查询(如下所示),由我的同事写。 t 是什么意思?另外,在查询中; 的作用是什么?我已经确定 t 不是任何表,也不是数据库中任何位置的任何字段。

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;


推荐答案

您的同事创建了一个查询> SELECT COUNT(*))和一个名为 t 的子查询。 t 只是一个临时表名称,指的是

Your colleague created a query (SELECT COUNT(*)) with a subquery that he named t. This t is just a temporary table name which refers to


SELECT AttemptNo FROM attempt_exercise 
  WHERE FK_UId = @uId AND FK_EId = @eId AND Mode = @mode


他可以将它命名为 temp 更清楚一些。这就像一张表的原因是,在MySQL中, SELECT 查询返回的数据行像表一样。因此,这个内部查询得到 AttemptNo ,并创建一个临时表 t

He could have feasibly named it temp to be a bit more explicit. The reason that this becomes like a table is because, in MySQL, a SELECT query returns rows of data which act like a table. So, this inner query gets the AttemptNo, and creates a temporary table t. The outer query then counts this data.

查询中的; 字符串查询由程序调用。如果不包括这些,则String 查询将不包含有效的MySQL语句。最后的; 是完成变量的赋值。

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 String query wouldn't contain a valid MySQL statement. The final ; is to complete the assignment for the variable.

这篇关于什么是“t”在这个SQL中参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆