I have this code to select distinct value and give me the number of values.
I want to select only the value in this month.
Example : give me all value of month 06 only in the same way distinct value number only in the month 06 or 05
Remarque: the date type in the database is varchar not date.
$sql=mysqli_query($conn,"select db_shopname, COUNT(db_shopname) as num FROM
(select tbl_order.db_shopname from tbl_order union all select tbl_item.db_shopname from tbl_item)t
GROUP BY db_shopname ")or die(mysqli_error($conn));
while($res=mysqli_fetch_array($sql)){
echo $res['db_shopname'];echo" ";echo"(";echo $res['num'];echo")";echo"<br/>";
}
To pull the month out of the varchar field use:
MONTH(STR_TO_DATE(db_date ,'%Y/%m%d'))=MONTH(NOW())
Add this into where condition
EDIT:
I don't know its work for what you expecting but try it .
select db_shopname, COUNT(db_shopname) as num
FROM
(select tbl_order.db_shopname
from
tbl_order
where MONTH(STR_TO_DATE(`db_date` ,'%Y/%m%d'))=MONTH(NOW())
union
all
select tbl_item.db_shopname
from tbl_item
where MONTH(STR_TO_DATE(`db_date` ,'%Y/%m%d'))=MONTH(NOW()))t
GROUP BY db_shopname
hi if you want to convert date varchar to date for searching you can use below example
select date_format(str_to_date('31/12/2010', '%d/%m/%Y'), '%Y-%m-%d') - return 2010-12-31
If you want only year
select date_format(str_to_date('31/12/2010', '%d/%m/%Y'), '%Y') - return 2010
If you want only month
select date_format(str_to_date('31/12/2010', '%d/%m/%Y'), '%m') - return 12
If you want only day
select date_format(str_to_date('31/12/2010', '%d/%m/%Y'), '%d') - return 31
Example
select count(name) from test where MONTH(str_to_date(date, '%d/%m/%Y')) = 06 group by name
Related
I have the following table with dummy values in mysql database:
id cnic amount depositDate receivedBy receivingZone remarks
1 11111 10000 01-Nov-2019 11111 1 Ok
2 11111 10000 07-Nov-2019 11111 1 ok
Now i want to get the sum of amount from the table for specific year (2019 in this case) where the year came from current timestamp (it may be 2020, 2021 etc depends on the current date)
Any help plz
You can use the YEAR function to get the year of the depositDate column and also the current year and then sum only the values which match:
SELECT SUM(amount) AS amount
FROM yourtable
WHERE YEAR(STR_TO_DATE(depositDate, '%d-%b-%Y')) = YEAR(CURDATE())
You can try below -
select sum(amount)
from tablename
where year(depositdate)=year(now())
I would write the WHERE clause to be sargable:
SELECT SUM(amount)
FROM yourTable
WHERE depositDate >= DATE_FORMAT(NOW() ,'%Y-01-01') AND
depositDate < DATE_FORMAT(DATE_ADD(NOW(), INTERVAL 1 YEAR) ,'%Y-01-01');
This approach, while a bit more verbose than the other answers which use the YEAR() function, would allow an index on the depositDate column to be used.
Based on your sample year, we need to recognize first the date using str_to_date
select sum(amount)
from tableA
where year(now()) = year(str_to_date(depositdate, '%d-%b-%Y'))
I am trying to get some record from table for specific month and year and I have only complete date field.
In the above image I want to select the data from date but only for (2015-09)
E.g.:
SELECT * FROM attendance WHERE date = (year-month)
You can use year and month function:
SELECT * FROM attendance WHERE year(`date`) = '2015' and month(`date`) = '9'
You can set your date range from the beginning and end of that month.
SELECT *
FROM attendance
WHERE `date` BETWEEN '2015-09-01' AND '2015-09-30'
Try to use DATE_FORMAT.
Your query is:
SELECT * FROM attendance WHERE DATE_FORMAT(date,'%Y-%m') = '2016-05'
I have a table which has product quantity column and the database has multiple entries in a single month.
Date format is (YYYY-MM-DD)
Date Quantity
2016-03-01 1200
2016-03-05 200
2016-04-05 500
2016-04-10 1000
2016-05-05 850
2016-05-10 50
So I want data as:
March (2016-03-01 to 2016-03-31) = 1400
April (2016-04-01 to 2016-04-30) = 1500
May (2016-05-01 to 2016-05-31) = 900
How can I do this?
Use the following query and it will return the result that you want.
' date field ' is the name of the column where date is inserted and Tablename is the name of the Table.
SELECT MONTH(date field) as month , YEAR(date field) as year , SUM(quantity) as
quantity FROM Tablename GROUP BY MONTH( date field )
select MONTH(Date) As dt , SUM(quantity) total from #Table1
GROUP BY MONTH(Date)
You can use DATE_FORMAT to convert the date field into just the year-month format, select the SUM of the quantity field, and then GROUP_BY the year-month field.
SELECT DATE_FORMAT('%Y-%m', `date`) as `ym`, SUM(`quantity`) FROM `table` GROUP BY `ym` ORDER BY `ym` ASC
You can try grouping by the month and year, taking the sum of the quantity field as an aggregate.
SELECT MONTH(Date), YEAR(Date), SUM(quantity)
FROM yourTable
GROUP BY MONTH(Date), YEAR(Date)
If you want the fancy output with actual date ranges, that would be a bit more work to do. Those dates may not exist in your original data set, so it could require a date table. And handling February in a leap year could be a real pain.
Use the following query same as it is it will return what you want
SELECT CONCAT(DATE_FORMAT(date,'%b'),' ',
(SELECT CONCAT('(',YEAR(date),'-',LPAD(MONTH(date),2,0),'-01 - ')
), (SELECT concat(last_day(date),')')
)) dates, SUM(quantity) qty FROM DATES GROUP BY MONTH(date) ORDER BY MONTH(date)
I want to select Registration per month.
<?php
include 'member_entry/sdg_connect.php';
$sql="select member_id from sdg_members where member_joindate BETWEEN DATE('2015-02-30') AND DATE( '2016-12-31')";
if ($result=mysqli_query($conn,$sql))
{
$rowcount=mysqli_num_rows($result);
echo $rowcount;
mysqli_free_result($result);
}
mysqli_close($conn);
?>
You can use an aggregate function count() and group by it by month
$sql="select count(member_id), month(member_joindate)
from sdg_members
where member_joindate BETWEEN str_to_date('30/02/2015', '%d/%m/%Y')
AND str_to_date('31/12/2016', '%d/%m/%Y')
group by month(member_joindate) ";
As scaisEdge already mentioned, you have to use group by:
select
YEAR(member_joindate) AS year, MONTH(member_joindate) AS month, COUNT(DISTINCT member_id) AS joins
from
sdg_members
where
member_joindate BETWEEN '2015-02-30' AND '2016-12-31'
group by
YEAR(member_joindate), MONTH(member_joindate)
This will give you results in the form of
year | month | joins
2015 | 3 | 10
2015 | 4 | 3
2015 | 5 | 11
...
Afterwards you do not need to apply num_rows anymore, you just have to go through the result set
If datatype of member_joindate column is just DATE than you can use this query:
SELECT COUNT(member_id), MONTH(member_joindate)
from sdg_members
where member_joindate BETWEEN '2015-02-30' AND '2016-12-31'
group by MONTH(member_joindate)
If datatype of member_joindate column is just DATETIME than you can use this query:
SELECT COUNT(member_id), MONTH(member_joindate)
from sdg_members
where member_joindate BETWEEN '2015-02-30 00:00:00' AND '2016-12-31 23:59:59'
group by MONTH(member_joindate)
Keep in mind MONTH() will return the month number, you can also use MONTHNAME() function.
SELECT COUNT(sb_id), MONTHNAME(sb_ondate)
from sbbleads_members
where sb_ondate BETWEEN '2015-02-30' AND '2016-12-31'
group by MONTHNAME(sb_ondate);
I have sql query
select
DISTINCT {fn MONTHNAME(entrydate)} AS monthname,
emirate_name,
location,
price,
bedroom
from itf_property
where
emirate_name='Dubai'
and location='JBR'
and bedroom='2'
group by monthname
order by id
I want to select the null month also from jan to dec like if jan have no value then it will show 0.
The output of above query is attached image
Use if on mysql
select
DISTINCT {fn MONTHNAME(entrydate)} AS monthname,
emirate_name,
if(month is null, '0', month) as month,
location,
price,
bedroom
from itf_property
where
emirate_name='Dubai'
and location='JBR'
and bedroom='2'
group by monthname
order by id