“t"是什么意思?在此 SQL 中引用? [英] What does "t" refer to in this SQL?

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

问题描述

我的代码中有一个简单的查询(如下所示),由我的同事编写.t 在这里是什么意思?还有 ; 在查询中的作用是什么?我很确定 t 不是任何表,也不是我数据库中任何地方的任何字段.猜猜这个查询有什么作用!!

string query = @"SELECT COUNT(*) FROM (SELECT AttemptNo FROM attempt_exerciseWHERE FK_UId = @uId AND FK_EId = @eId AND Mode = @mode)t;";

代码如下(如果需要任何其他信息):

MySqlCommand cmd = new MySqlCommand(query, _conn);cmd.Parameters.AddWithValue("@uId", uId);cmd.Parameters.AddWithValue("@eId", eId);cmd.Parameters.AddWithValue("@mode", mode);尝试计数 = Convert.ToInt32(cmd.ExecuteScalar());_conn.Close();返回尝试计数;

解决方案

你的同事创建了一个查询 (SELECT COUNT(*)) 和一个他命名为 t 的子查询.这个t只是一个临时表名,引用

<块引用>

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

他本可以将它命名为 temp 以便更明确一点.这变得像一个表的原因是,在 MySQL 中,SELECT 查询返回的数据行就像一个表.因此,这个内部查询获取AttemptNo,并创建一个临时表t.然后外部查询计算这些数据.

查询中的;是为了在程序调用string查询时使其成为一个完整的语句.如果不包括它,则字符串 query 将不包含有效的 MySQL 语句.最后的;就是完成对变量的赋值.

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 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

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.

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天全站免登陆