I have a script written in PHP which reports sales activity.
FOLLOWING currently fetching just values. Today Yesterday Last 7 days.
Today for field use the following code:
if($x==0) {
$sql="SELECT SUM(incasat) FROM tichete WHERE DATE(datainchis) = DATE(NOW()) AND inchisde='$y'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
if(!empty($row['SUM(incasat)'])) echo $row['SUM(incasat)']; else echo '0';
}
Field yesterday to use the following code:
if($x==1) {
$sql="SELECT SUM(incasat) FROM tichete WHERE datainchis BETWEEN DATE_ADD(CURDATE(), INTERVAL -1 day) AND CURDATE() AND inchisde='$y'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
if(!empty($row['SUM(incasat)'])) echo $row['SUM(incasat)']; else echo '0';
}
I would like to do like this. I wish I could show collections on each and every day like Today Yesterday 3 days ago 3 days but for 3 days ago i want to see like yesterday not amount just pays from this day.
Enclose the code.
function get_user_incasari($x,$y)
{ // 0 - azi, 1 - ieri, 7 - ultimele 7 zile, 30 - luna asta, 31 - luna trecuta
if($x==0) {
$sql="SELECT SUM(incasat) FROM tichete WHERE DATE(datainchis) = DATE(NOW()) AND inchisde='$y'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
if(!empty($row['SUM(incasat)'])) echo $row['SUM(incasat)']; else echo '0';
}
if($x==1) {
$sql="SELECT SUM(incasat) FROM tichete WHERE datainchis BETWEEN DATE_ADD(CURDATE(), INTERVAL -1 day) AND CURDATE() AND inchisde='$y'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
if(!empty($row['SUM(incasat)'])) echo $row['SUM(incasat)']; else echo '0';
}
if($x==3) {
$sql="SELECT SUM(incasat) FROM tichete WHERE datainchis BETWEEN DATE_ADD(CURDATE(), INTERVAL -3 day) AND CURDATE() AND inchisde='$y'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
if(!empty($row['SUM(incasat)'])) echo $row['SUM(incasat)']; else echo '0';
}
if($x==7) {
$sql="SELECT SUM(incasat) FROM tichete WHERE datainchis BETWEEN DATE_ADD(CURDATE(), INTERVAL -7 day) AND CURDATE() AND inchisde='$y'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
if(!empty($row['SUM(incasat)'])) echo $row['SUM(incasat)']; else echo '0';
}
if($x==30) { //luna curenta
$sql="SELECT SUM(incasat) FROM tichete WHERE MONTH(datainchis) = MONTH(CURDATE()) AND inchisde='$y'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
if(!empty($row['SUM(incasat)'])) echo $row['SUM(incasat)']; else echo '0';
}
if($x==31) { //luna precedenta/trecuta
$sql="SELECT SUM(incasat) FROM tichete WHERE MONTH(datainchis) = MONTH(CURDATE() - INTERVAL 1 MONTH ) AND inchisde='$y'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
if(!empty($row['SUM(incasat)'])) echo $row['SUM(incasat)']; else echo '0';
}
}
You can use timestamps (like your datainchis column), truncated to date, in GROUP BY queries. It's easy. For example, this gives you a summary, by day, starting eight days ago and ending yesterday. (Note: CURDATE() means DATE(NOW()).)
SELECT SUM(incasat) incasat_total, DATE(datainchis) datainchis
FROM tichete
WHERE inchisde='$y'
AND datainchis >= CURDATE() - INTERVAL 8 DAY
AND datainchis < CURDATE()
GROUP BY DATE(datainchis) WITH ROLLUP
Notice, please, that your code contains an error (a common error).
If you have timestamps and you say
datainichis BETWEEN CURDATE() - INTERVAL 1 DAY AND CURDATE()
you get all the rows with timestamps starting with midnight yesterday, up to and including midnight today. You actually want to exclude midnight today; those records belong to today, not yesterday.
datainichis >= CURDATE() - INTERVAL 1 DAY
AND datainichis < CURDATE()
Notice the < in place of <=.
Your code also contains an inefficiency. The expression
DATE(datainichis) = CURDATE()
is not sargable. It defeats the use of an index on the datainichis column.
I have written a little essay on this topic, here.
Related
I need to check which plant are online so I'm thinking to compare last date time update to the current time if difference is less than 15 min the plant is online otherwise is offline
I have write this in php to make a query but all row change to online or offline what I'm doing wrong?
it seams that the update is done on all row of the database not only in the selected by the first query
$sql = "select * from PlantData where lastUpdate > DATE_SUB(NOW(), INTERVAL 15 MINUTE)";
$resultt = mysqli_query($connection, $sql);
//echo($resultt);
while ($roww = $resultt->fetch_array()) {
$rowws[] = $roww;
}
foreach($rowws as &$roww) {
$sqlupdate = "update PlantData set statusConnection = 'online'";
mysqli_query($connection, $sqlupdate);
}
$sql2 = "select * from PlantData where lastUpdate < DATE_SUB(NOW(), INTERVAL 15 MINUTE)";
$resultt2 = mysqli_query($connection, $sql2);
//echo($resultt);
while ($roww2 = $resultt2->fetch_array()) {
$rowws2[] = $roww2;
}
foreach($rowws2 as &$roww2) {
$sqlupdate2 = "update PlantData set statusConnection = 'offline'";
mysqli_query($connection, $sqlupdate2);
}
thanks for support
Run the query in only once
UPDATE PlantData
SET
statusConnection = CASE
WHEN lastUpdate >= NOW() - INTERVAL 15 MINUTE THEN 'online'
WHEN lastUpdate < NOW() - INTERVAL 15 MINUTE THEN 'offline'
END;
Are these both query same :
SELECT count(site_key) as count_lic FROM (`activation`)
WHERE `site_key` = '36LYRAHK0100000000B6101442Cs' AND `user_id` = '' AND `request_time` = 'BETWEEN NOW() - INTERVAL 30 DAY AND NOW()
SELECT count(site_key) as count_lic FROM (`activation`) WHERE `site_key` = '36LYRAHK0100000000B6101442Cs' AND `user_id` = '' AND `request_time` BETWEEN NOW() - INTERVAL 30 DAY AND NOW()
Can we use 'request_time' = 'BETWEEN.......' ?
I am using PHP Codeigniter and I have done :
function fGetUsedLicenses($site_id) {
//count number of activations without user_id
$this->db->select('count(site_key) as count_lic');
$this->db->where('site_key', $site_id);
$this->db->where('user_id','');
$this->db->where('request_time', 'BETWEEN NOW() - INTERVAL 30 DAY AND NOW()');
$this->db->from('activation');
$query = $this->db->get();
$result = $query->row();
$count = $result->count_lic;
echo $this->db->last_query();
//count number of activations with unique user_id
$this->db->select('count(DISTINCT user_id) as count_lic');
$this->db->where('site_key', $site_id);
$this->db->where('user_id !=','');
$this->db->where('request_time', 'BETWEEN NOW() - INTERVAL 30 DAY AND NOW()');
$this->db->from('activation');
$query = $this->db->get();
$result = $query->row();
$count += $result->count_lic;
var_dump($count);
echo $this->db->last_query();
exit(0);
return $count;
}
Now If I use this way the query generated is 'request_time' 'BETWEEN.....'
You can write it like
->where('request_time BETWEEN NOW() - INTERVAL 30 DAY AND NOW()', "", false);
i have this function :
function get_user_dep($userid)
{
$sql = mysql_query("SELECT COUNT(userid) AS total_dep FROM `userdep` WHERE `type` LIKE 'Depozitare' AND DATE(`date`) <= NOW() AND DATE(`date`) >= DATE_SUB(NOW(), INTERVAL 7 DAY)");
$row = mysql_fetch_array($sql);
$count = $row['total_dep'];
if ($count > 1) {
return 'Yes';
} else {
return 'No';
}
}
with database userdep
id/userid/type/date/amouth
i made with assoc, numrow , and it returns me No , and in the database i have more than 4 entrys in interval 7 days.
Sorry for having to ask this in an answer - my rep is too low. What field type is your date column? From what I just read in your comments on the answer below, the DATE field requires 'yyyy-mm-dd'.
1) SQL is valid.
2) Change this:
$row = mysql_fetch_array($sql);
to this:
$row = mysql_fetch_array($sql, MYSQL_ASSOC);
or to this:
$row = mysql_fetch_assoc($sql);
3) Also you can try this to check what you received back from DB.
echo print_r($row, true);
I have a table called 'main_table' with 3 columns :
'player', 'points' and 'drop_date'
I have 1 variable ($date) with different values:
$date == '2012-06-01'
$date == '2012-05-01'
$date == '2012-04-01'
I Have 1 MySQL query:
$query = "
select *
from main_table
where `drop_date` > '$date'
AND `drop_date` <= DATE_ADD('$date', INTERVAL 1 YEAR)
LIMIT 1
";
GOAL :
I would like to run ONE query with different passes (1pass per value)
I have tried :
<?php
$date['date'] = '2012-06-01';
$date['date'] = '2012-05-01';
$date['date'] = '2012-04-01';
foreach($date as $title => $actual_date) {
query = "
select *
from main_table
where `drop_date` > '$actual_date'
AND `drop_date` <= DATE_ADD('$actual_date', INTERVAL 1 YEAR)
LIMIT 1
";
$result = mysql_query($query) or die(mysql_error());
}
while($row = mysql_fetch_array($result)) {
echo $row['Player'];
echo $row['Points'];
}
You keep overwriting the same variable over and over... and then you run the query but only fetch results for the last one. How do you expect it to work?
Try this:
$date = Array("2012-06-01","2012-05-01","2012-04-02");
foreach($date as $actual_date) {
if( $result = mysql_fetch_assoc(mysql_query("select * from `main_table` where `drop_date`>'".$actual_date."' and `drop_date`<=date_add('".$actual_date."',interval 1 year) limit 1"))) {
echo $result['Player'];
echo $result['Points'];
}
}
Note that I skipped putting the query in a variable, and putting the query result in a variable, and just one-lined the whole thing. Since you have limit 1 the query will only return one row, so there is no need to while-loop it.
Just move your while in the foreach loop.
<?php
$date[0] = '2012-06-01';
$date[1] = '2012-05-01';
$date[2] = '2012-04-01';
foreach($date as $title => $actual_date)
{
query = "select * from main_table where `drop_date` > '$actual_date' AND `drop_date` <= DATE_ADD('$actual_date', INTERVAL 1 YEAR) LIMIT 1";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['Player'];
echo $row['Points'];
}
}
Do not overwrite your values. Do this to keep all 3 values.
<?php
$date['date'][] = '2012-06-01';
$date['date'][] = '2012-05-01';
$date['date'][] = '2012-04-01';
print_r($date);
?>
OUTPUT:
Array
(
[date] => Array
(
[0] => 2012-06-01
[1] => 2012-05-01
[2] => 2012-04-01
)
)
Then use
foreach ($date['date'] as $actual_date) {
$query = "
select *
from main_table
where `drop_date` > '$actual_date'
AND `drop_date` <= DATE_ADD('$actual_date', INTERVAL 1 YEAR)
LIMIT 1";
echo $query."<br />";
}
OUTPUT:
select *
from main_table
where `drop_date` > '2012-06-01'
AND `drop_date` <= DATE_ADD('2012-06-01', INTERVAL 1 YEAR)
LIMIT 1
<br />
select *
from main_table
where `drop_date` > '2012-05-01'
AND `drop_date` <= DATE_ADD('2012-05-01', INTERVAL 1 YEAR)
LIMIT 1
<br />
select *
from main_table
where `drop_date` > '2012-04-01'
AND `drop_date` <= DATE_ADD('2012-04-01', INTERVAL 1 YEAR)
LIMIT 1
<br />
I have a sql table : date (Y-m-d) / time (00:00:00) / power (INT)
When I select a date from an inline datepicker, I am trying to post 3 HighCharts graph (one-24 hours, two-31 days of month, three-12 months of year) and I need to get the values out of the table for the chart to be created.
For the day, I need the 24 values for each hour '100,200,300,200,300 etc..'
Here is the PHP for the "day" but it is not working...
<?php
$choice = (isset($_POST['choice']))
? date("Y-m-d",strtotime($_POST['choice']))
: date("Y-m-d");
$con = mysql_connect("localhost","root","xxxxxx");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("inverters", $con);
$sql = "SELECT HOUR(time), COUNT(power)
FROM feed
WHERE time = DATE_SUB('".$choice."', INTERVAL 24 HOUR)
GROUP BY HOUR(time)
ORDER BY HOUR(time)";
$res = mysql_query($sql) or die('sql='.$sql."\n".mysql_error());
$row = mysql_fetch_assoc($res);
echo $row['choice'].'<br />';
?>
This has been confirmed by another individual that the code does not work, would anyone have a helpful solution to fix the error ?
Alan
At the moment, your SELECT gives you only the results which happened exactly 24 hours before the current moment. What you need is a range. Example for 1 hour (indentation added for clarity):
WHERE `time` BETWEEN
DATE_SUB('".$choice."', INTERVAL 24 HOUR)
AND DATE_SUB('".$choice."', INTERVAL 23 HOUR)
This way, you'll get results with time in the 1-hour range of "now - 24 hours" and "now - 23 hours". The BETWEEN operator is equivalent to this:
WHERE `time` >= DATE_SUB('".$choice."', INTERVAL 24 HOUR)
AND `time` <= DATE_SUB('".$choice."', INTERVAL 23 HOUR)
You need a loop to walk over the rows:
$sql = "
SELECT HOUR(time) as h, power
FROM feed
WHERE date = '".$choice."'
ORDER BY HOUR(time)";
$res = mysql_query($sql) or die('sql='.$sql."\n".mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo $row['h'].":".$row['power']'<br />';
}
This will give you the power per day for a given month:
$sql = "
SELECT DAY(date) as d, SUM(power) as powerday
FROM feed
WHERE MONTH(date) = '".$month."'
GROUP BY DAY(date)
ORDER BY DAY(date)";
$res = mysql_query($sql) or die('sql='.$sql."\n".mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo $row['d'].":".$row['powerday']'<br />';
}
Thank you everyone for all your help !
The problem was in the first string, I only had to change the date format in addition to your wonderful examles !
$choice = (isset($_POST['choice'])) ? date("m",strtotime($_POST['choice'])) : date("m");
Thank You Very Much !
Alan