如何在mysql中通过id查询父id命令

时间:2016-10-24 06:12:41

标签: mysql sql

(1) Table Name Groups
| ID | parent_id| Name                                |
|  1 |      NULL|      Assets                         |
|  2 |      NULL|      Liabilities and Owners Equity  |
|  3 |      NULL|      Incomes                        |
|  4 |      NULL|      Expenses                       |
|  5 |      1   |      Fixed Assets                   |
|  6 |      1   |      Current Assets                 |
|  7 |      1   |      Current Assets                 |
|  8 |      2   |      Capital Account                |
|  9 |      2   |      Current Liabilities            |
|  10|      2   |      Loans (Liabilities)            |
|  11|      3   |      Direct Incomes                 |
|  12|      4   |      Direct Expenses                |
|  13|      3   |      Indirect Incomes               |
|  14|      4   |      Indirect Expenses              |
|  15|      3   |      Sales                          |
|  16|      4   |      Purchases                      |
|  17|      6   |      Cash at Bank                   |
|  18|      17  |      KWL Banks                      |

(2) Table Name ledger

|  ID|      GroupID |      Name         |
|  1|       18      |      1708-6 ABL   |


I need query result like below
--------------------------------------------------
Assets
    Current Assets
            Cash at Bank
                  KWL Banks
                        1708-6 ABL
      Fixed Assets
      Investments
--------------------------------------------------
Liabilities and Owners Equity
      Capital Account
      Current Liabilities
      Loans (Liabilities)
--------------------------------------------------
Incomes
      Direct Incomes
      Indirect Incomes
      Sales
--------------------------------------------------
Expenses
      Direct Expenses
      Indirect Expenses
      Purchases

0 个答案:

没有答案