How to create tree based query from same table in mysql? - php

Hey guys i want to create one tree based data in my html view from mysql.
I have one table salary_slip there 2 columns pay_month and pay_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.
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.
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 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.
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.
SQL Query:
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
$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.
SQL Query to get years:
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:
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

Related

PHP MYSQL get column based on date month

I don't even know how to ask this question...
So there is a database in place in which the columns are the months of the year:
January | February | March
Then each column has it's values:
jan1 | feb1 | mar1
jan2 | feb 2 | mar2
jan3 | feb3 | mar3
Sorry, I didn't know how to insert a table in Stack Overflow.
So when I run a query, I was wondering if there was a way mysql can automatically identify the current month, and use that to select the corresponding column.
So if the current month is January, then the SELECT would look like this:
$select = "SELECT UID, JANUARY FROM table WHERE UID = '1'";
That would bring back the column data for January where the UID = 1.
But when it's February, the query needs to automatically adjust the SELECT statement to select February.
I am not even sure if this is possible. I just wanted to see if anyone might know if there is a way to do this.
Use PHP built-in date() function, specified here.
date("F") // returns full string representation of current month like "January"
The code would then look something like this:
$select = "SELECT UID, ".date("F")." FROM table WHERE UID = '1'";
If you want a pure SQL solution, go for this:
select uid,
case month(curdate())
when 1 then january
when 2 then february
when 3 then march
when 4 then april
when 5 then may
when 6 then june
when 7 then july
when 8 then august
when 9 then september
when 10 then october
when 11 then november
when 12 then december
end as month_data
from mytable
where uid = '1';
Here is a fiddle.
MySQL has no function similar to eval(), so you have to include some long code like #trincot's answer. But if you don't mind using a prepared statement it can be done in a clearer way (IMHO):
set #s = CONCAT('SELECT ', MONTHNAME(NOW()), ' FROM meses');
PREPARE stmt FROM #s;
EXECUTE stmt;

How to get year just before current year from MySQL table

I need a record whose year is just before the current year. My table schema and data is like this
aca_id|aca_start_date | aca_end_date
------|---------------|--------------
1 |2013-04-01 |2014-03-31
3 |2015-04-01 |2016-03-31
7 |2014-04-01 |2015-03-31
I want to fetch the record depending on the current year condition is - if current date year is 2014 then fetch the 1st record because its aca_start_date is 2013 which just before 2014. If current date year is 2015 then fetch the record 2nd record. I cannot sort the table on aca_start_date because table is sorted on aca_id in descending order for other purpose.
Need guidance how can I do this job in MySQL query or if this is possible to do by using php
Here is one way:
select t.*
from table t
where year(aca_start_date) = year(now()) - 1
limit 1;

Month wise sum of column mysql

Hello i am using php and mysql.I have one table with one column as recdatetime as datetime datatype .
If i search record between two months i want sum of quantity column of same table.
If i have following record.
quantity recdatetime
**44** `2014-01-01 16:53:06`
**14** ` 2014-01-21 16:53:06`
**10** `2013-12-21 16:53:06 `
**17** `2013-12-22 16:53:06 `
**29** `2013-11-20 16:53:06`
If i search between November 2013 and January 2014 i want output in following manner.
November December January
29 27 58
I want Mysql query for above output.
Start with this:
SELECT SUM(quantity) AS s, DATE_FORMAT(recdatetime, '%M') AS m
FROM table_name
GROUP BY DATE_FORMAT(recdatetime, '%Y-%m')
You can use month(recdatetime), sum(quantity) and group by month(recdatetime).
Select year(recdatetime), month(recdatetime), sum(quantity) as total from x
group by year(recdatetime),month(recdatetime);
You will need the year or it will also sum up months for different years. You normaly do not want this. And add a where condition to only get the months you want. Hope you can do that on your own.
Switching rows and columns could be done in the output.
You can try like this:
select sum(quantity) as sumQty,recdatetime FROM table_name group by Month(recdatetime

how to fetch the data according to particular month & year in mysql php

Ok, i have a database in which i have all my users registered with their datetime which stores in my MYSQL datetime field as 2012-03-31 12:13:42 now i want to fetch COUNT of users according to year and month.
As i want to present a chart data for that purpose, i want total customers added in January, in feb, march and so on.... in particular year.
i.e it has to be year specific that in 2010 Jan lets say 52 Customers in 2010 Feb lets say 72 Customers.
Note: The query i want to execute is using PHP and Mysql.
You can use the MySQL date functions
SELECT YEAR(date_field) as stat_year,
MONTH(date_field) as stat_month,
COUNT(*) as stat_count
FROM `table`
GROUP BY stat_year,stat_month
If you only want the results for a specific year/month, you can add conditions to the WHERE clause (and remove the GROUP BY) like this
SELECT COUNT(*) as stat_count
FROM `table`
WHERE YEAR(date_field) = 2012
AND MONTH(date_field) = 3
There are different SQL that will do the trick, here is one ...
SELECT COUNT(dt), DATE_FORMAT(dt, '%Y-%b') AS df FROM foo GROUP BY df;
where foo is your table name, and dt is the datetime column. Note that this will apply the function DATE_FORMAT on each row. Also, you can change the format as desired, e.g. use %m for numeric month, %M for full month name, etc. Add in where clause to filter as needed. The following SQL is almost the same ...
SELECT COUNT(dt), YEAR(dt) AS y, MONTH(dt) AS m FROM foo GROUP BY y, m;

Get Current year and next form mysql

Im am selecting various things from a table. The problem is I only want to select them if they belong to the current year and the next year.
My table looks like this
Title Text Date
The date is formated like this 0000-00-00 and is in the format "date"
So the question is how can i select only items from only this year and the next?
Example: the year is 2012, I have items in my table that is old and i dont want them to show - I only want to select items from at the first 2012 1 January and last in this case 31 Dec 2013 current year 2012 + 1 year.
Thanks a lot for any help!
SELECT
*
FROM
table
WHERE
YEAR(date) = YEAR(CURDATE())
OR
YEAR(date) = YEAR(CURDATE()) + 1
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
SELECT
*
FROM
table
WHERE
date BETWEEN CONCAT(YEAR(CURDATE()),'-01-01') AND CONCAT(YEAR(CURDATE())+1,'-12-31')
As ugly as it looks, it allows your query to use index on date field.
Better idea is to create limiting dates in external PHP script, so that the query can use query cache.
If you only want to show items, that are no older than two years, you can do this:
SELECT
*
FROM
table
WHERE
date >= CURDATE() - INTERVAL 2 YEAR;

Categories