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

报告相同问题?

悬赏问题

  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作