简体   繁体   English

如何从mysql中的同一表创建基于树的查询?

[英]How to create tree based query from same table in mysql?

Hey guys i want to create one tree based data in my html view from mysql. 大家好,我想在mysql的html视图中创建一棵基于树的数据。 I have one table salary_slip there 2 columns pay_month and pay_year . 我有一张表salary_slip有两列pay_monthpay_year

data is like this: 数据是这样的:

pay_month      pay_year

February         2014
march            2014
January          2015
February         2015
April            2015
may              2015
June             2015
January          2016
march            2016
December         2016

So now i want to create tree view for the same.so, if user will click on 2016(pay_year) onclick this all data in pay_month in front of 2016 should be display in tree format and same for the all. 所以现在我想为same.so创建树视图,因此,如果用户单击2016(pay_year)onclick,则2016年前pay_month中的所有数据都应以树格式显示,并且全部相同。

My current query is: 我当前的查询是:

select 
    PAY_MONTH,
    PAY_YEAR 
from 
    india_salary_slip_details 
where 
    EMPLOYEE_ID = 34 
group by 
    pay_year 

By this i got my data: but i can only see each year's first month only. 这样我得到了我的数据:但是我只能看到每年的第一个月。 please provide me with better query so i can get my result. 请为我提供更好的查询,以便我得到我的结果。

image for the same: current data from my query 相同的图片: 查询中的当前数据

A major part of your problem comes from the use of GROUP BY , which is used for, well, grouping. 问题的主要部分来自使用GROUP BY ,它用于分组。

MySQL has different handling of GROUP BY than other DBMS' that require all non-aggregate fields to be listed in the GROUP BY clause. MySQL对GROUP BY处理方式与其他DBMS的处理方式不同,其他DBMS要求在GROUP BY子句中列出所有非聚合字段。 MySQL give you enough rope with which to hang yourself (so to speak), which is why you didn't get an error for what is clearly not an intended use of GROUP BY . MySQL为您提供了足够的绳索来吊起自己(可以这么说),这就是为什么您没有因为显然不是GROUP BY的预期用途而得到错误的原因。

Depending on how your front end is handling the data, you have a few options. 根据前端处理数据的方式,您有几种选择。

Option 1) Have it load (ordered) and use the output loop to determine the change in year. 选项1)加载(排序)并使用输出回路确定年份的变化。

SQL Query: SQL查询:

SELECT 
    PAY_MONTH,
    PAY_YEAR 
FROM 
    india_salary_slip_details 
WHERE 
    EMPLOYEE_ID = 34 
ORDER BY 
    pay_year,
    pay_month -- we want it ordered by year, then month, though this will be alphabetical due to the data type

PHP Code: PHP代码:

<?php
$year = NULL;
foreach ($result as $row) { // Assuming your result is stored as $result
   if ($row['PAY_YEAR'] !== $year) {
       // Print the year in the left column so to speak, perhaps create a sublist, etc
       // Set the year 
       $year = $row['PAY_YEAR'];
   }
   // Add month
   // Do something with $row['PAY_MONTH'], perhaps add it to a sublist, again, depends on front end handling.
}

Option 2) Have it load the years, then the next level asynchronously. 选项2)加载年份,然后异步加载下一个级别。

SQL Query to get years: SQL查询获取年份:

SELECT DISTINCT
    PAY_YEAR 
FROM 
    india_salary_slip_details 
WHERE 
    EMPLOYEE_ID = 34 
ORDER BY
    PAY_YEAR

SQL Query to get months given a year: SQL Query获得一年的月份:

SELECT 
    PAY_MONTH
FROM 
    india_salary_slip_details 
WHERE 
    EMPLOYEE_ID = 34 
    AND PAY_YEAR = (Whatever the year is)
ORDER BY 
    PAY_MONTH

The problem is your group by, you just want to order by year : 问题是您的分组依据,您只想按年份订购:

SELECT PAY_MONTH, PAY_YEAR, FROM india_salary_slip_details WHERE EMPLOYEE_ID = 34
ORDER BY PAY_YEAR

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

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