Home  >  Q&A  >  body text

mysql怎么查询树形结构的数据

现有id,pid,name三个字段,要查找id为某值下面的所有数据,应该怎么写效率最高

怪我咯怪我咯2741 days ago599

reply all(4)I'll reply

  • PHPz

    PHPz2017-04-17 16:27:15

    select a.*,b.name pname from table a,table b where a.pid=b.id and b.id=3

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 16:27:15

    SELECT fileds FROM table_name WHERE pid=id; 

    If the amount of data is not large, the program can recursively traverse the query spanning tree;

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 16:27:15

    Oracle has a syntax that supports tree queries. For MySQL, you can only process it through code

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 16:27:15

    Use php+mysql to implement through recursion
    Portal:
    http://blog.csdn.net/u0102656...

    reply
    0
  • Cancelreply