dtnnpt11795 2011-03-13 00:29
浏览 47
已采纳

有人可以向我解释这个SQL查询吗?

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
  • 写回答

7条回答 默认 最新

  • douzhang5984 2011-03-13 00:34
    关注
    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案