简体   繁体   English

如何对三个结构数据应用SQL LIMIT?

[英]How apply SQL LIMIT for a three structure data?

I use a famous method for showing the three comment from SQL. 我使用一种著名的方法来显示来自SQL的三个注释。 How I can display only first three root comment with a all sub-comment? 如何只显示前三个带有所有子注释的根注释?

I tried to make something like: 我试图做这样的事情:

  SELECT * FROM NAMES  ORDER BY pid LIMIT 3 ASC, Id ASC LIMIT 3 

But LIMIT is not supported SQL for each ORDER BY 但是每个ORDER BY都不支持LIMIT SQL

The example you can see here: http://sqlfiddle.com/#!2/a593d/4 您可以在此处看到示例: http : //sqlfiddle.com/#!2/a593d/4

More clear I illustrated in loaded image 我在加载的图像中显示的更加清晰

The LIMIT keyword in SQL limits the amount of rows that is returned. SQL中的LIMIT关键字限制了返回的行数。 It there is a sort involved SQL will be smart enough to stop the ordering after the first three rows are found. 它涉及一种排序,SQL将很聪明,足以在找到前三行后停止排序。

If you want to do something else with the LIMIT keyword then you should explain what exactly it is you want to get. 如果您想使用LIMIT关键字做其他事情,那么您应该解释一下您到底想要得到什么。

Try below query 尝试以下查询

  SELECT * 
  FROM NAMES INNER JOIN 
     (SELECT pid 
      FROM NAMES
      GROUP BY pid
      ORDER BY pid
      LIMIT 3) AS my_table 
  USING (pid)

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM