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;
Related
how can i find last date record in MySQL.
i have this current date record but if i want to get a data of last day (not yesterday). it is possible to get last date of record in MySQL.
my current date record are there (28-11-2017)..
and i want last date of record which is (25-11-2017)
between this 25 to 28 there is no data . i want this last date record.
ex.
id DATE PRODUCT_ID
1 2017-11-25 11
2 2017-11-25 12
3 2017-11-24 6
so last date in my table will be 25. how can i get all record of that date.
link for current date table: current table
One option is to compare the date of each record against a non correlated subquery which finds the most recent date in the table.
SELECT *
FROM yourTable
WHERE DATE = (SELECT MAX(DATE) FROM yourTable);
If you are certain that there is only one latest date, or you can live with just a single record in the event of ties, then MySQL offers an even simpler option:
SELECT *
FROM yourTable
ORDER BY DATE DESC
LIMIT 1;
SELECT DATE FROM your_table ORDER BY id DESC LIMIT 1
This will get you the last date.
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
I got stuck with my query. I need to show monthly (current + previous) records.
Let's take below example for the year 2014:-
I have 7 records in the month 03(March)
I have 2 records in the month 04(April)
So total records in the year 2014 is 9.
Now we come to the year 2015:-
I need to show all records in Jan(No new records added in the database), This should be 9.
My Query shows 0.
I need to show all records in Feb(5 new records added in the database), This should be 14.
My query shows 5.
Query works fine when I am checking for month 03 and year 2014, showing all 7 records. but not for all conditions.
I am calling the same query for fetching all months(01-12) data one by one.
SELECT count(pkID) as TRecords
FROM students
WHERE (fkCategoryID ='56'
OR fkSecondaryCategoryID ='56'
OR fkOptionalCategoryID ='56')
AND MONTH(`DateAdded`) <='1'
AND YEAR(`DateAdded`) <='2015';
AND MONTH(`DateAdded`) <='1'
Think about this. You won't find a month smaller than 1. Year and month belong together; don't look at them separately.
SELECT COUNT(pkID) as TRecords
FROM students
WHERE (fkCategoryID = 56 OR fkSecondaryCategoryID = 56 OR fkOptionalCategoryID = 56)
AND DATE_FORMAT(DateAdded, '%Y%m') <= '201501';
In you where clause, you are doing <= (less than or equal to). I expect you want just = (equal to) for your year and month. Certainly your year should only be = (equal to).
I want to create a query for fetch all the data from database. But condition is get from before current month and data will convert into 3 days slot wise with count.Like if i have 12 month data and in July 1 to 3 date data will insert into 50 rows and from 3 to 6 date data will insert into 5 rows.How i fetch this things.That is the mail problem. I have wondered but nothing found related my problem. Is this possible.
I assume you have a DATE or a DATETIME column, that marks the day of the insert, say the column created_at. Then you could get the desired result.
SELECT
COUNT(*)
FROM
your_table
WHERE
created_at BETWEEN '2014-07-01' AND '2014-07-31 23:59:59'
GROUP BY
(DAYOFMONTH(created_at) - 1) DIV 3
Explanation:
With the help of the DIV operator you get your 3-day-slots.
Note
If there's a slot without rows, it won't be in the result. To get these too, you could use a LEFT JOIN with the maximum 11 slots.
Demo
If you having timestamps as attribute type then might be this help you
SELECT count(*) as count
FROM table
AND
TIMESTAMPDIFF(DAY,date,2014-08-01)<=3";
The date is the attribute of your column, TIMESTAMPDIFF will find difference between given date if it's under 3 days from 2014-08-01 records will show up. This way by just sending one date into this position you can find 3 slots of date from your table.
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;