English 中文(简体)
Can someone explain this SQL query to me?
原标题:

I m reading this article and I m trying to understand this SQL statement but I am still somewhat new to SQL.

I m not sure what comment and c refer to.
I think one of them is the table name but I am not sure of the other. Also, apparently there is a subquery within it which I have not had any experience with:

  SELECT c.id, c.user_id, c.body, c.deep, c.lineage, c.parent_id,
         (SELECT COUNT(*) 
            FROM comment 
           WHERE comment.lineage LIKE (CONCAT(c.lineage, % )) 
             AND comment.lineage != c.lineage) AS replies
    FROM comment as c
ORDER BY c.lineage
最佳回答
SELECT c.id,
       c.user_id,
       c.body, 
       c.deep, 
       c.lineage, 
       c.parent_id, (
       SELECT COUNT(*)
         FROM comment
        where comment.lineage LIKE (CONCAT(c.lineage, % ))
          AND comment.lineage!=c.lineage)
       as replies
       FROM comment as c 
       order by c.linea

The first list are all the fields to be selected, with the prefix of c which is the alias later to the comment table.

The query in a query is a subquery, which runs that query which does a like and concatenates .clineage with % (which is the wildcard). This subquery result is saved in replies.

The results are ordered by linea.

问题回答

c is an alias for a table named comment defined with comment as c.

comment is indeed the name of a table in this query. c is an alias used for that table (in the syntax comment as c) so that elsewhere in the query the comment table can be referenced with simply a c instead of the entire table name.

In this particular case, where the sub-query is also querying from the same table, the alias allows it to reference that same table from the parent query. This is useful here because, in the context of the sub-query, c.lineage is a static value (per row returned from the parent query) which is used to filter rows in the sub-query (with comment.lineage). The sub-query can then return a single value per row of the parent query, and that value is aliased to the name replies in the result.

"comment" is the table name, and "c" is just an alias for it to save typing. The query gets a list of comments from the comments table. It returns a number of columns specified by c.id, c.user_id, c.body, c.deep, c.lineage, c.parent_id, as well as the number of replies to this comment, as specified by (SELECT COUNT(*) FROM comment where comment.lineage LIKE (CONCAT(c.lineage, % )) AND comment.lineage!=c.lineage) as replies

Comment is a table and c is an alias for the last comment table reference. So, c.id refers to the id column in the last instance of the comment table.

You were very close in what you thought. comment is the table name, and so is c. See the line that says FROM comment as c that is labeling the comment as c. The subquery is everything inside those outer ()

The as keyword creates an alias for something so that you can refer to it later unambiguously. So, comment refers to the table and c is an alias for that same table. This is especially useful since you re referring to comment in two different contexts (in both the main query and the subquery).

It also allows you to assign the name replies to the result of your subquery:

(SELECT COUNT(*) 
   FROM comment 
  WHERE comment.lineage LIKE (CONCAT(c.lineage, % )) 
    AND comment.lineage!=c.lineage) as replies




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签