MySQL - DATE_ADD month interval by each month - mysql

I have written a function below to give me the number of jobs an employee has done in a particular 30 day period (each ID in the 'jobs column' of the table represents 1 job).
the function work fine if I only want to look back 4 week. the problem however is that I want the count to start at the beginning of each month. for example, if a person views the records on the 10th December 2013 I need the records to show all the work for December (but not the records for the preceding 30 days).
Below is my function:
$interval_1month = 'interval 4 WEEK';
function statsHowMuchWorkDoneByStaff ($staff_id, $timeInterval)
{
global $dbc;
$select = " SELECT
COUNT(job_id) AS totalnumberWork ";
$from = " FROM
staffwork
";
$where = " WHERE
staff_id = $staff_id
AND
FROM_UNIXTIME(entrytime) >= now() - $timeInterval";
$query = $select.$from. $where;
$result = mysqli_query ($dbc, $query)
or trigger_error("Query: $query\n<br />MySQL Error: " . mysqli_error($dbc));
if(mysqli_num_rows($result))
{
$row = mysqli_fetch_array ($result, MYSQLI_ASSOC);
$result = safe_output($row['totalnumbernewcontacts']) ;
return $result ;
}
else
{
return false;
}
}
Any advise on how to proceed would be greatly appreciated.
UPDATE: here is my datatable:
CREATE TABLE staffwork(
staff_id MEDIUMINT UNSIGNED NOT NULL,
job_id MEDIUMINT UNSIGNED NOT NULL,
data_table VARCHAR (65) NOT NULL,
entrytime int(11) NOT NULL,
INDEX message (staff_id)
);

If I understand correctly and you want to calculate COUNT(job_id) for a specific month by supplying any date of that month as a parameter, then you can do it this way
SELECT COUNT(job_id) total
FROM staffwork
WHERE staff_id = 1
AND entrytime >= UNIX_TIMESTAMP(LAST_DAY('2013-12-10') + INTERVAL 1 DAY - INTERVAL 1 MONTH)
AND entrytime < UNIX_TIMESTAMP(LAST_DAY('2013-12-10') + INTERVAL 1 DAY)
Note: This query is index friendly because it doesn't convert entrytime to datetime but rather convert range values (which are constants for the query) to unix time. Make sure that you have indices on entrytime and staff_id to be able to take advantage of that.
Here is SQLFiddle demo
And while you're at it consider to learn and use prepared statements instead of interpolating query strings leaving your code vulnerable for sql injections.
That being said your php function might look like this
function statsWorkDoneByStaffMember($staff_id, $month) {
global $dbc;
$sql = "
SELECT COUNT(job_id) total
FROM staffwork
WHERE staff_id = ?
AND entrytime >= UNIX_TIMESTAMP(LAST_DAY(?) + INTERVAL 1 DAY - INTERVAL 1 MONTH)
AND entrytime < UNIX_TIMESTAMP(LAST_DAY(?) + INTERVAL 1 DAY)
";
$stmt = $dbc->prepare($sql);
if (!$stmt) {
trigger_error('Prepare failed: ' . $dbc->error);
}
$stmt->bind_param('iss', $staff_id, $month, $month);
if(!$stmt->execute()) {
trigger_error('Execute failed: ' . $dbc->error);
}
$stmt->bind_result($result);
$stmt->fetch();
$stmt->close();
return $result;
}
Sample usage:
$dbc = new mysqli('localhost', 'user', 'password', 'dbname');
$staff_id = 1;
//Get the number of job_id for the current month
$total = statsWorkDoneByStaffMember($staff_id, date('Y-m-d'));
//Get the number of job_id for a specific month
$total = statsWorkDoneByStaffMember($staff_id, '2013-07-01');

Related

show mysql average of the last 14 days in a c# application

I have a mysql table with the columns ID, name, gold and timestamp.
This table shows with the following statement the average of the gold of the respective item(name). But now the database is already filled with 90k entries and the average should only be limited to 2 weeks. Here is the code:
`
private DataTable GetAverageGold()
{
var dtAverageGold = new DataTable();
var percentageBuy = double.TryParse(textBox1.Text.Replace("%", string.Empty), out var sell) ? sell / 100 : 0.02;
using (_con = GetMySqlConnection())
{
using (var cmd = new MySqlCommand(
"SELECT name AS name, FORMAT(GROUP_CONCAT(gold ORDER BY id DESC), 'de_DE') AS 'MOST RECENT', FORMAT(ROUND(AVG(gold) - AVG(gold) * " +
percentageBuy.ToString(CultureInfo.InvariantCulture).Replace(",", ".") +
"), 'de_DE') AS Einkaufspreis, FORMAT(ROUND(AVG(gold)),'de_DE') AS Durchschnitt, ROUND((AVG(gold) - GROUP_CONCAT(gold ORDER BY id DESC)) / GROUP_CONCAT(gold ORDER BY id DESC) * 100,2) AS 'profit in %' FROM items GROUP BY name ORDER BY Name",
_con))
{
try
{
_con.Open();
var reader =
cmd.ExecuteReader();
dtAverageGold.Load(reader);
}
catch (MySqlException ex)
{
MessageBox.Show(ex.ToString(), "Error in: GetAverageGold", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
finally
{
_con.Close();
}
}
}
return dtAverageGold;
}
`
I tried "WHERE timestamp <= NOW() + INTERVAL 14 DAY GROUP BY name ORDER BY name" at the end of the statement. but it did not help. The syntax seems to be correct, as I get no error, but the average still refers to all entries
You were thinking along the right lines but it should have been -
WHERE timestamp >= NOW() - INTERVAL 14 DAY
or
WHERE timestamp >= CURRENT_DATE - INTERVAL 14 DAY

Selecting rows that are within 2 hours from current time

I am using PHP with MySQL and would like to select rows that have a booking time within 2 hours from now. How do I compare what is in my database with the NOW() MySQL function?
I have columns pickupDate in the format yyyy-mm-dd and pickupTime in the format HH:mm (24-hour). I have tried creating a query with NOW() which returns the a 12-hour time as HH:mm:ss e.g. 2019-05-24 07:54:06 . I can't figure out how to format this to 19:54, or if I should use a different function instead.
For example, if the current date and time is 24/05/19 19:54:06, I would like to select rows between 19:54 and 21:54 on this date.
My table structure is:
referenceNo VARCHAR(100)
pickupDate DATE
pickupTime VARCHAR(100)
You need to create a DATETIME compatible value out of your pickupDate and pickupTime (which you can do by CONCATing them together), then you can compare that with a time range from NOW() to 2 hours later:
SELECT *
FROM yourtable
WHERE CONCAT(pickupDate, ' ', pickupTime) BETWEEN NOW() AND NOW() + INTERVAL 2 HOUR
Demo on dbfiddle
To add two hours in php
$hoursnow = date('H:i');
$timestamp = strtotime(date('H:i')) + 60*60*2;
$plusTwohours = date('H:i', $timestamp);
And $PlusTwohours using this variable frame the query like below
Sql Query:
$sqlQuery = 'select * from foodorder where pickupDate=DATE(NOW()) AND pickupTime>='.$hoursnow.' and pickupTime<='.$plusTwohours;
$result = mysql_query($sqlQuery);
variable $result will have the values of query
For Second Scenario: Adding hours to end of the day May 24 23:30:00
This should be handle by two different date for same column pickupDate
$d = new DateTime('2011-01-01 23:30:30');
$startDate = $d->format('Y-m-d H:i:s'); // For testing purpose assigned manually
$starttime = date('H:i');
// Here Process start, storing end date by adding two hours
$enddate1 = strtotime($startDate) + 60*60*2;
$enddate = date('Y-m-d', $enddate1); // Extracting date alone
$endtime = date('H:i', $enddate1); // Extracting time alone
Have to compare start and end date for column pickupDate, here is the query
$sqlQuery = "select * from foodorder where pickupDate>=DATE(".$startDate.") AND pickupDate<=DATE(".$enddate.") AND pickupTime>='".$starttime."' AND pickupTime<='".$endtime."'";
$result = mysql_query($sqlQuery);

how can i loop two colum values

I have a table named leaves.
----------
id FromDate ToDate
1 20-01-2019 22-01-2019
2 15-01-2019 22-01-2019
3 13-01-2019 20-01-2019
I want all dates between each column.
Can anyone help?
If you want to do it in your php code then you can do it by finding day count between two days and loop it to get the all dates between that two dates.
<?php
$date1 = "2019-01-13";
$date2 = "2019-01-20";
$date1 = strtotime("2019-01-13");
$date2 = strtotime("2019-01-20");
$datediff = $date2 - $date1;
$days = round($datediff / (60 * 60 * 24));
for($i=1;$i<=$days;$i++){
echo $date1 = date('d-m-Y', strtotime($date1 . ' +1 day'));echo ' <br> ';
}
You can try below using datediff() function
select id, fromdate, todate,datediff(ToDate,fromdate) as days
from tablename

Cannot Get MYSQL DATEADD to Add The Year

This is really puzzling me as DATEADD should work and it isn't and wondered if anyone knew why. Here is my statement:
$r = mysqli_query($con,"SELECT ID, DATEADD(year,1,BEGINDATE) AS NEXTYEAR FROM b_crm_deal");
while($row = mysqli_fetch_array($r))
{
print "".$row['NEXTYEAR']."<br />";
}
This doesn't return anything. If I was to return the BEGINDATE it is:
2015-08-04 00:00:00
I basically want NEXTYEAR to return 2016-08-04 00:00:00. I've tried the different combinations of year, yyyy, yy and nothing is returning.
DATEADD isn't a valid function in MySQL (it's MSSQL), use date_add instead:
DATE_ADD(BEGINDATE, interval 1 year) AS NEXTYEAR
See the manual for more information.
DATEADD looks wrong to me, see https://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_adddate
so try:
$r = mysqli_query($con,"SELECT ID, ADDDATE(BEGINDATE, INTERVAL 1 YEAR) AS NEXTYEAR FROM b_crm_deal");
while($row = mysqli_fetch_array($r))
{
print "".$row['NEXTYEAR']."<br />";
}

Convert MySQL fetch array query to redbean PHP

I have 2 buttons which execute a post operations and set a hidden variable which is used to set the MySQL query to filter the database according to date
if result = today
$query = "SELECT id,customer_name,CAST( `register_date` AS DATE ) AS dateonly,status,
DATE_FORMAT(book_date, '%m/%d/%y') FROM table WHERE book_date
BETWEEN (CURDATE() - INTERVAL 1 DAY) AND CURDATE()";
if result = week
$query = "SELECT id,customer_name,CAST( `register_date` AS DATE ) AS dateonly,status,
DATE_FORMAT(book_date, '%m/%d/%y') FROM table
WHERE book_date BETWEEN (CURDATE() - INTERVAL 7 DAY) AND CURDATE()";
I then want to use something like
$result=mysql_query($query);
while ($mytable=mysql_fetch_array($result))
{
loop and display all the information in array in a table
}
But I need the red bean equivalent of this.
The easiest way is to just paste the $query inside the sql function:
$results=R::getAll($query);
foreach($results as $row){
echo $row['id'];
}
The next way is to manually build the query.... which may just make it look sloppier in my opinion:
$results=R::$f->begin()->select('id, customer_name, CAST( register_date AS DATE ) AS dateonly,status, DATE_FORMAT(book_date, '%m/%d/%y')')->from('table')->where('book_date BETWEEN (CURDATE() - INTERVAL 1 DAY) AND CURDATE())->get();
The final way is to grab results via redbean and handle them manually:
$results=R::find('table','book_date BETWEEN (CURDATE() - INTERVAL 7 DAY) AND CURDATE()');
Then loop through the results, configuring data along the way in php.
I always use this when I have to access a lot of data from mysql:
while ($row = mysqli_fetch_array($query)) { #converts query into array
$array[] = $row;
}
$array will be a multidimensional array. $array[x][column_name] will get you your data, x being the row which you want to access it from. Hope this helped.