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);
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;
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.
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've following query which updated the db table parkings to column name : latitude, longitude, parking_time and let_parked. So It's update all the column but it's not updating the parking_time column with NOW(). Do you know why ?
if(isset($_POST["update"]) && $_POST["update"]==true)
{
if($userid == "1")
{
$parking_time = "(NOW() - INTERVAL 122 MINUTE)";
$results = $mysqli->query("UPDATE parkings SET latitude = '$mLat', longitude = '$mLng', parking_time = '$parking_time', let_parked = '1' WHERE locId = '$mId' ");
}
elseif($userid == '0')
{
$parking_time = "NOW() - INTERVAL 220 MINUTE";
$results = $mysqli->query("UPDATE parkings SET latitude = '$mLat', longitude = '$mLng', parking_time = '$parking_time' WHERE locId = '$mId' ");
}
if (!$results) {
//header('HTTP/1.1 500 Error: Could not Update Markers! $mId');
echo "coudld not update marker." . mysql_error();
exit();
}
exit("Updated successfully Done! $userid, $mId");
}
Remove the () from around your date value, e.g.
$parking_time = "(NOW() - INTERVAL 122 MINUTE)";
should be
$parking_time = "NOW() - INTERVAL 122 MINUTE";
and then remove the ' from around where you use that datetime:
UPDATE [snip], parking_time = $parking_time,[snip]
^-- ^-- no quotes
This will eventually produce
UPDATE ... parking_time = NOW() - INTERVAL 122 MINUTE, ....
instead of
UPDATE ... parking_time = '(NOW() - INTERVAL 122 MINUTE)' ...
Your version was generating a string which contained some text. But since that text was treated as a string, MySQL would NOT execute the NOW() - ... date math, so you were trying to literally set your db field value to an invalid date.
I really need help on this one. I'm trying to do a mysql update based uisng case but i don't think i'm getting it right.
Here is what i'm trying to achieve.
I have a table with the following fields
user_id, rank, weekly, monthly, justwinners
1 9 0 0 0
2 29 0 0 0
3 8 0 0 0
4 10 0 0 0
5 12 0 0 0
What i want to achieve is to update the weekly, monthly and justwinners fields based on specific dates
Lets say i have a start date of 5/07/2011.
$startdate = 5-07-2011;
At the end of one week which is 12/07/2011 i want to update the weekly field of the user with highest rank to 1. Also at the same time update justwinners
field of the next 3 users with the highest rank to 1.
At the end of the second week which is now 19/07/2011, i want to update the weekly field of the user with highest rank whose value is still '0' to 2 and also update
the justwinners field whose value is still '0' to 2.
This will continue until i get to one months time when i will update the monthly field.
This is what i have been able to come up with so far which is not working.
//Initiate the database connection here
function get_db_conn() {
$conn = mysql_connect(HOST, DB_USER, DB_PASSWORD) or die('Could not Connect!');
mysql_select_db(DATABASE, $conn) or die ('could not connect to database');
return $conn;
}
function updateWinners( $limit, $field ) {
$conn = get_db_conn();
switch($field) {
case "weekly" :
$limit = 1;
case "monthly" :
$limit = 1 ;
case "giftpack" :
$limit = 10 ;
default:
$limit = 1 ;
}
$sqlquery = "SELECT * FROM application rank DESC " ;
$sqlquery .= " where $field < 1 ";
$sqlquery .= " LIMIT $limit ";
$result = mysql_query($sqlquery);
$user_data = mysql_fetch_row($result);
if(isset($user_data)) {
$user_data = 0 ;
while($user_data){
$uid = $user_data(user_id);
$rank = $user_data(rank);
$query = "INSERT INTO application_winners (user_id, rank, date) VALUES ('$uid', '$rank' 'now()')";
mysql_query($query) or die('Error, insert winners query failed');
switch ($action) {
case "weekly":
$startdate = "5-07-2011";
$sqlquery = "UPDATE application SET weekly = CASE
WHEN (CURDATE() = (startdate * 7))
THEN weekly = '1'
WHEN (CURDATE() = (startdate * 14))
THEN weekly = '2'
WHEN (CURDATE() = (startdate * 30))
THEN weekly = '3'
ELSE weekly
END";
}
$user_data = $user_data + 1;
} //endwhile
}
else {
echo "Error in updating the winners";
}
}
In case you know of any better way to implement this. kindly share.
Just as a remark - you have an insert followed by an update. You could do this in the insert part. Something like:
$query = "INSERT INTO application_winners (user_id, full_name, rank, date, $field) VALUES ('$uid', '$user_fullname', '$rank' 'now()', '$updatevalue')";
mysql_query($query) or die('Error, insert winners query failed');
i did not look at everything closely but you are using the variable "$action" for the second switch but it does not exist anywhere else in your code..
Try changing:
$sqlquery = "UPDATE application SET weekly = CASE
WHEN (CURDATE() = (startdate * 7))
THEN weekly = '1'
WHEN (CURDATE() = (startdate * 14))
THEN weekly = '2'
WHEN (CURDATE() = (startdate * 30))
THEN weekly = '3'
ELSE weekly
END";
to:
$sqlquery = "UPDATE application SET weekly = CASE
WHEN (CURDATE() = (startdate * 7))
THEN '1'
WHEN (CURDATE() = (startdate * 14))
THEN '2'
WHEN (CURDATE() = (startdate * 30))
THEN '3'
ELSE weekly
END";