如何获得所有父母的身份证

时间:2019-01-07 07:28:42

标签: mysql hierarchical-data

我有一个包含类别列表的MySQL表。

    
    category_id         name                parent_id
    --------------------------------------------------
    1                    beverage           NULL
    2                    Water              1
    3                    Sparkling Water    2
    4                    Snacks             NULL
    5                    Chips              4
    
    
    product_id      name            category_id
    -------------------------------------
    1               water001        3   
    2               Chips001        5   
    

如何通过一个查询获得类别路径?

SELECT name as product_name,category_path FROM product_table
    
    product_name    category_path
    -------------------------------------
    water001        3,2,1
    Chips001        5,4
    

1 个答案:

答案 0 :(得分:-1)

我认为您需要一个存储过程。我认为您无法使用单个SQL语句来做到这一点。

我对编写SQL过程并不熟悉,但是像这样的somtehintg可能会有所帮助:

public class Main {

    public static void main(String[] args) {

        Calendar calendar = Calendar.getInstance();

        Date selectedDate = null;
        Date currentDate = null;


        //Formatter to format the date given by system
        SimpleDateFormat requiredFormant =  new SimpleDateFormat("yyyy-MM-dd");


        //To get current Date
        try {
            currentDate = requiredFormant.parse(requiredFormant.format(calendar.getTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        // user inputted Text ,it can be from EditText Etc
        try {
             selectedDate = requiredFormant.parse("2019-01-09");
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //Logic to compare dates
        if (currentDate.equals(selectedDate))
            System.out.println("You born today ?");
        else if(currentDate.after(selectedDate))
            System.out.println("Good to proceed");
        else
            System.out.println("Not Possible");


    }

}

请检查正确的拼写!!

要获得结果,您需要类似:

SELECT GetPath(123);

但是要小心:当过程永远找不到“ NULL”时,此解决方案可能会以不结束循环结尾!

希望我能帮上忙!

相关问题