I need to query a database which will return the number of people subscribed to a particular service during that month.
I use the below query
select subscriberid, count(*) from ABC where updated time like '2013-05-%' ;
In this query I need to update the Updatedtime field to be 2013-06-% when the next month comes and then 07 when the next to next month comes. I want the query to be updated automatically when the next month comes instead of manually changing it every time.
Also note that I want the data for a particular month at a time, so please don't suggest grouping by month as an answer.
One way to do it
SELECT subscriberid, COUNT(*)
FROM ABC
WHERE YEAR(updated_time) = YEAR(CURDATE())
AND MONTH(updated_time) = MONTH(CURDATE())
or
SELECT subscriberid, COUNT(*)
FROM ABC
WHERE updated_time BETWEEN ADDDATE(LAST_DAY(SUBDATE(CURDATE(), INTERVAL 1 MONTH)), 1)
AND LAST_DAY(CURDATE())
The following should work fine:
SELECT
subscriberid,
count(*)
from
ABC
where
updatedtime LIKE CONCAT(DATE_FORMAT(NOW(),'%Y-%m'), '-%')
I think you can use DATE_FORMAT function
SELECT subscriberid, count(*) as total
FROM ABC
WHERE DATE_FORMAT(updated_time, "%Y-%m") = "2013-05";
Use the following query:
SELECT subscribersid, updated, COUNT(*) from subscribers
WHERE YEAR(updated) = YEAR(NOW())
AND MONTH(updated) = MONTH(NOW())
You can see it working in this SQL Fiddle
Hope this helps
Related
I want to retrieve the records of employees who were joined in first quarter or in the first month. I have tried this but am not getting the right answer...
SELECT * FROM table
WHERE DOJ(date_created) = DOJ(CURRENT_DATE - INTERVAL 1 MONTH)
Please help me with this!
Answering the question as clarified in a comment...
SELECT * FROM table
WHERE YEAR(table.doj) = 2015 AND QUARTER(table.doj) = 1
If instead you want "first quarter of prior year"...
SELECT * FROM table
WHERE YEAR(table.doj) = YEAR(CURRENT_DATE) - 1 AND QUARTER(table.doj) = 1
In either case, note that there's no code to include the first month, because that's part of the first quarter. However, if you wanted to make that explicit (at a slight performance hit), you could code it as follows...
SELECT * FROM table
WHERE YEAR(table.doj) = 2015 AND (QUARTER(table.doj) = 1
OR MONTH(table.doj) = 1)
If you run into performance problems because you have a lot of records but only an index on table.doj, you could also write the query over an explicit date range...
SELECT * FROM table
WHERE table.doj >= '2015-01-01' AND table.doj <= '2015-03-31'
I am trying to order mysql query by month name like:
January -- 5
February -- 2
March -- 5
and so on
Here is my query, but its not ordering:
SELECT leave_balance.balance, MonthName(leave_balance.date_added) AS month
FROM leave_balance WHERE leave_balance.staff_id_staff = $iid
GROUP BY month, leave_balance.leave_type_id_leave_type
HAVING leave_balance.leave_type_id_leave_type = $leaveBalTypID
ORDER BY month
Kindly tell me where I am doing wrong
you can specify like
ORDER BY FIELD(MONTH,'January','February','March',...)
SELECT leave_balance.balance, MonthName(leave_balance.date_added) AS month
FROM leave_balance WHERE leave_balance.staff_id_staff = $iid
GROUP BY month, leave_balance.leave_type_id_leave_type
HAVING leave_balance.leave_type_id_leave_type = $leaveBalTypID
ORDER BY FIELD(MONTH,'January','February','March',...,'December');
Just add this at the end of your query statement:
ORDER BY str_to_date(MONTH,'%M')
If column name changes in the future you will not need to worry about it.
try using the built in month function to get the month's number and order by that. For example:
order by month(leave_balance.date_added)
simply order by leave_balance.date_added field. By default it will sort by month.
I am trying to nest a few queries but so far am getting back error 1242: Subquery returns more than 1 row. I want more than one row, as I am working on a number of records.
I have 2 tables. One has a commencement date stored in 3 columns; yr_comm, mth_comm, day_comm. The 2nd table has a period of service (in years) for a number of users which is expressed as an integer (2.71, 3.45, etc).
I need to take this start date (from table 1), and add on the period of service (from table 2) to obtain an end date, but I only need to display the year.
I have 2 queries which work just fine when seperate, they result in the required values, however I am having trouble combining the queries to get the desired end result.
Query 1: Concatenate the 3 commencement values into date format
SELECT concat_ws('-', yr_comm, mth_comm, day_comm) AS date_comm
FROM table 1
Query 2: Convert the integer yrs_service into days
SELECT format(yrs_served * 365, 0) AS days_served
FROM table 2
Query 3: Use date_add function to add the days service to the commencement date
SELECT date_add(date_comm, INTERVAL days_served DAY) AS date_left
Can anyone suggest how I can achieve the above? Many thanks in advance.
EDIT - Here is the full query I am working on:
SELECT prime_minister.pm_name, yr_comm, party, ADDDATE(
(SELECT CONCAT_WS('-', yr_comm, mth_comm, day_comm) FROM ministry), INTERVAL
(SELECT FORMAT(yrs_served * 365, 0) FROM prime_minister) YEAR) AS date_left
FROM ministry JOIN prime_minister USING (pm_name)
WHERE party NOT LIKE '%labor%'
AND prime_minister.pm_name = ministry.pm_name
ORDER BY pm_name;
you can use user variables
SET #date = CONCAT_WS('-', 2012,1,1); -- paste your query here
SET #toAdd = (SELECT MONTH(CURDATE())); -- paste your query here
SELECT DATE_ADD(#date, INTERVAL #toAdd DAY) AS date_left
SQLFiddle Demo
which is the same as
SET #date = CONCAT_WS('-', 2012,1,1); -- paste your query here
SET #toAdd = (SELECT MONTH(CURDATE())); -- paste your query here
SELECT #date + INTERVAL #toAdd DAY AS date_left
SQLFiddle Demo
or without using variable, which is more longer,
SELECT (CONCAT_WS('-', 2012,1,1)) + INTERVAL (SELECT MONTH(CURDATE())) DAY AS date_left
SQLFiddle Demo
I have mysql database and is full of over 2 years of data. How can I make a query in a way that it will get the result as below?:
January
Week 1
...
data rows here
....
Week 2
...
...
Week 3
...
...
Week 4
...
...
February (same as above)
These are the field structures:
- date (in yyyy-mm-dd format)
- job (integer autoincrement)
- person (varchar)
Try this, it will generate output pretty similar to one required by you
SELECT MONTH(date) AS MONTH, WEEK(date) AS WEEK, DATE_FORMAT(date, %Y-%m-%d) AS DATE, job AS JOB, person AS PERSON GROUP BY WEEK(date);
GROUP BY year(record_date), MONTH(record_date)
Check out the date and time functions in MySQL.
It has to be a query, or you can use ajax too? Using ajax you could do a loop in javascript:
date = [initial date]
while date <= [final date] {
divUpdate = 'divrecs'; // will be used in updateDiv function
url = 'getrecs.php?date1='+date+'&date2='+(date+6);
conecta(url,updateDiv);
date = date+7;
}
And inside getrecs your query would be:
$stmt = $dbh->prepare("select * from yourtable where date >= '".$_GET['date1']."' and date <= '".$_GET['date2']."'");
if ($stmt->execute()) {
while ($row = $stmt->fetch(PDO::FETCH_BOTH)) {
echo "Do anything with your data here: $row[1] etc<BR>";
}
}
Hope that helps!
SELECT
MONTH(created_datetime) AS month,
WEEK(created_datetime) AS week,
DATE(created_datetime) AS date,
COUNT(id) AS count
FROM users
WHERE date(created_datetime) BETWEEN '2017-05-01' AND '2017-12-31'
GROUP BY month
ORDER BY created_datetime
If you want records date wise that you need to use GROUP BY date.
If you want records weekly that you need to use GROUP BY week.
If you want records monthly that you need to use GROUP BY month.
How can I get records from my table using month/year? I have a table like this:
Name - varchar
DueDate -datetime
Status -boll
DueDate is project due date, I want record corresponding to month/year, not full date, I mean record for specific month.
How can I do this in mysql?
Simply use MONTH() and YEAR():
SELECT * FROM Project WHERE MONTH(DueDate) = 1 AND YEAR(DueDate) = 2010
You could use a between statement:
select * from Project
where DueDate between '2010-01-01' and '2010-02-01'
Do not use this here recommended solution with MONTH() and YEAR(). It prevents MySQL to use index on column DueDate if you have one and it forces MySQL to examine all rows in table.
Instead, use BETWEEN clause like this:
SELECT * FROM Project
WHERE DueDate BETWEEN '2010-01-01' AND '2010-02-01'
Or another solution with IN clause:
SELECT * FROM Project
WHERE DueDate IN ('2010-01-01', '2010-01-02', '2010-01-03', ..., '2010-01-31')
This two solutions allows MySQL to use index, so performance will be better.
SELECT Name FROM Table Where MONTH(datetime) = 1;
1 = January.
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_month
it will select current year's specific month
SELECT * FROM Project WHERE MONTH(DueDate) = 1 AND YEAR(DueDate) = YEAR(NOW())
If you input month format "Y-m" you can use:
SELECT * FROM Project WHERE DATE_FORMAT(DueDate,"%Y-%m") = '2010-01'
You can extract the MONTH() and YEAR() for your DueDate.
SELECT * WHERE MONTH(DueDate) = '5' AND YEAR(DueDate) = '1987';
SELECT * FROM TABLE WHERE DueDate LIKE '2020-01%';
use in case you can substring your date data.