There was a problem with the request. Any ideas?
The situation is as follows, there is a request
MERGE ore as T_Base USING ( SELECT
date
,sec
,shift
,id_ore
FROM ore
WHERE date = '2022-9-20'
AND sec = '2'
AND shift = '1') AS T_Source
ON T_Base.date = T_Source.date
AND T_Base.sec = T_Source.sec
AND T_Base.shift = T_Source.shift
AND T_Base.id_ore = T_Source.id_ore
WHEN MATCHED
THEN UPDATE
SET date = T_Source.date
,sec = T_Source.sec
,shift = T_Source.shift
,id_ore = '1'
WHEN NOT MATCHED
THEN INSERT (date, sec, shift, id_ore)
VALUES (T_Source.date, T_Source.sec, T_Source.shift, '1');
But for some reason it returns "0 rows affected".
Table structure:
[id] [int] IDENTITY(1,1) NOT NULL,
[date] [datetime] NULL,
[sec] [int] NULL,
[shift] [int] NULL,
[id_ore] [int] NULL
Values in the table:
id date sec shift id_ore
11637 2022-09-10 00:00:00.000 1 1 13
11636 2022-09-09 00:00:00.000 1 2 13
etc
Expected Result
When updated, no change
id date sec shift id_ore
11637 2022-09-10 00:00:00.000 1 1 13
1.1) When update, with changes (shift and id_ore updated )
id date sec shift id_ore
11637 2022-09-10 00:00:00.000 1 2(change) 27(change)
When new values are inserted:
id date sec shift id_ore
11638 2022-09-20 00:00:00.000 1 1 1
Sorry for this post. I misunderstood the MERGE construction. If there is no data in any of the tables, the operation will not be performed, since there will be nothing to match
If suddenly, someone encountered a similar, came to the following solution:
IF EXISTS (SELECT
date
,sec
,shift
FROM ore
WHERE date = '" . $y . "-" . $m . "-" . $d . "'
AND sec = '" . $sec . "'
AND shift = '" . $shift. "')
UPDATE ore
SET id_ore = '" . $nam_ryd . "'
WHERE date = '" . $y . "-" . $m . "-" . $d . "'
AND sec = '" . $sec . "'
AND shift = '" . $shift. "'
ELSE
INSERT ore (date, sec, shift, id_ore)
VALUES ('" . $y . "-" . $m . "-" . $d . "', '" . $sec . "', '" . $shift. "', '" . $nam_ryd . "')
Related
I am trying to take insert hours and minutes from MySQL in OpenCart
instead updating the input value its update 12:00 AM for all the time when i click for save individually
here is the query
$this->db->query("UPDATE " . DB_PREFIX . "delivery_time SET delivery_time_name = '" . $this->db->escape($data['delivery_time_name']) . "', delivery_time_from = 'TIME( STR_TO_DATE( " . $this->db->escape($data['delivery_time_from']) . ", \"%h:%i %p\" ) )', delivery_time_to = '" . $this->db->escape($data['delivery_time_to']) . "', status = '" . (int)$data['status'] . "' WHERE delivery_time_id = '" . (int)$delivery_time_id . "'");
but same working on phpmyadmin
UPDATE `oc_delivery_time` SET `delivery_time_name`="Slot2",`delivery_time_from`=TIME( STR_TO_DATE( "9:30 AM", "%h:%i %p" ) ),`delivery_time_to`=TIME( STR_TO_DATE( "12:00 PM", "%h:%i %p" ) ),`status`="1" WHERE `delivery_time_id`="2"
Use MySQL STR_TO_DATE or interval
STR_TO_DATE('1:00 PM', ' %h:%i')+ interval 12 hour
or try this query :-
Select STR_TO_DATE('1:00 PM', ' %h:%i')+ interval 12 hour
You should use %H instead of %h, wrong pattern
h stands for 00 to 12
H for 00 to 23
http://www.techonthenet.com/mysql/functions/str_to_date.php
I have the following query:
INSERT INTO `impressions` (`date`, `item_id`, `platform`, `country`) VALUES ('" . $date . "', '" . $item_id . "', '" . $platform . "', '" . $country . "') ON DUPLICATE KEY UPDATE `impressions` = `impressions` + 1
And the following array:
Array
(
[0] => 5
[1] => 2
[2] => 4
[3] => 17
)
The array basically consists of item_ids, which I want to insert/update in to the database.
When the query runs, I want it to check to see if there are any rows which are from: today, match the specificed item_id, match the specified platform and match the specified country.
For example:
2015-03-05 5 mobile US (new insert)
2015-03-05 2 mobile UK (new insert)
2015-03-05 5 mobile US (this would +1 impression from first one)
2015-03-05 17 desktop US (new insert)
2015-03-06 5 mobile US (this would create a new insert because the date doesn't exist)
I also want to pass an array of item_ids, as specified above using IN, to avoid multiple looping.
Right now we are currently doing it like this:
$item_ids = array('5', '2', '4', '17');
foreach($item_ids as $id){
$q = mysql_query("SELECT `id` FROM `impressions` WHERE `date` = '" . $date . "' AND `item_id` = '" . $id . "' AND `platform` = '" . $platform . "' AND `country` = '" . $country . "'");
if (mysql_num_rows($q)){
$r = mysql_fetch_array($q);
mysql_query("UPDATE `impressions` SET `impressions` = `impressions` + '1' WHERE `id` = '" . $r['id'] . "'");
} else {
mysql_query("INSERT INTO `impressions` (`date`, `item_id`, `platform`, `country`, `impressions`) VALUES('" . $date . "', '" . $id . "', '" . $platform . "', '" . $country . "', '1') ");
}
}
We basically want to convert this huge (and badly coded) loop, into 1 mySQL INSERT/UPDATE query which gets passed an array of item_ids to be used with IN.
I am really confused how I should approach this. Any help would be greatly appreciated.
UPDATE impressions = impressions + 1
needs to be
UPDATE impressions = VALUES(impressions) + 1
INSERT INTO `impressions` (
`date`,
`item_id`,
`platform`,
`country`,
`impressions`
)
VALUES (
'{$date}',
{$id},
'{$platform}',
'{$country}', '1')
ON DUPLICATE KEY
UPDATE `impressions` = VALUES(impressions) + 1
for this to work, see to it that the table impressions has a primary key to check with.
so that the on duplicate key it will update it
I have the following script that retrieve numbers from 2 tables, make a sum, and the value is updated into a 3th table.
$query = "SELECT (SELECT SUM(net_amount) FROM fin_costos WHERE month='1' AND group_of_costos='general' AND year_analysis='2014' ) +
(SELECT SUM(net_amount) FROM em2_fin_costs WHERE month='1' AND group_of_costos='general' AND year_analysis='2014') AS total";
$result = mysqli_query($mysqli,$query);
while($row = mysqli_fetch_array($result)){$valor_final = $row['total']; }
$query_update="UPDATE fusion_analysis SET jan='$valor_final' WHERE year_for_analysis='2014' AND item_for_analysis='general' AND category='general'";
$result = mysqli_query($mysqli,$query_update);
I need to run the same script for each month of the year. Everything is exaclty the same except the variable 'month' that changes from 1 to 12 and the SET in UPDATE where the value is uploaded for each month ('jan','feb', 'mar'...etc)
I'm currently just copying and pasting the same script changing this few parameters but I believe there is a smarter way to do this in less lines of code I have
See date function of PHP:
$query = "SELECT (SELECT SUM(net_amount)"
. " FROM fin_costos"
. " WHERE month='".date('n')."'"
." AND group_of_costos='general' AND year_analysis='".date("Y")."' ) +"
."(SELECT SUM(net_amount) FROM em2_fin_costs WHERE month='".date('n')."'"
. " AND group_of_costos='general' AND year_analysis='".date("Y")."') AS total";
$query_update="UPDATE fusion_analysis"
. " SET `". strtolower(date('M'))."`='$valor_final'"
. " WHERE year_for_analysis='".date("Y")."'"
. " AND item_for_analysis='general'"
. " AND category='general'";
NOTE:
Y - A full numeric representation of a year, 4 digits like 2014
n - Numeric representation of a month, without leading zeros 1 - 12
M - A short textual representation of a month, three letters Jan through Dec
For month as short textual I've used the strtolower function to make it lowercase.
UPDATE
Based on OP comment:
for ($i = 1; $i <= 12; $i++) {
$query = "SELECT (SELECT SUM(net_amount)"
. " FROM fin_costos"
. " WHERE month='" . $i . "'"
. " AND group_of_costos='general' AND year_analysis='" . date("Y") . "' ) +"
. "(SELECT SUM(net_amount) FROM em2_fin_costs WHERE month='" . $i . "'"
. " AND group_of_costos='general' AND year_analysis='" . date("Y") . "') AS total";
$result = mysqli_query($mysqli, $query);
$row = mysqli_fetch_assoc($result);
$valor_final = $row['total'];
$monthName = strtolower(date('M', strtotime(date("Y") . "-" . str_pad($month,2, "0", STR_PAD_LEFT) . "-" . date("01") )));
$query_update = "UPDATE fusion_analysis"
. " SET `" . $monthName . "`=' " . $valor_final . "'"
. " WHERE year_for_analysis='" . date("Y") . "'"
. " AND item_for_analysis='general'"
. " AND category='general'";
mysqli_query($mysqli, $query_update);
}
Hey guys I've tried to figure this out,
I have
$query = "SELECT URL
FROM Matches
WHERE match_date = '" . $currentdate . "'
AND play_datetime < '" . $currenttime . "'
AND DATE_ADD(play_datetime, INTERVAL 3 HOUR) > '" . $currenttime . "'";
I can get the first AND to return, but the second part wont work where i have DATE_ADD, ive tried it by itself to return anything and i cant get it to work!
$current time right now is 06:01:14, its = date(h:i:s);
then in play_datetime it picks up the match_date lists a result, the time in play_datetime is 04:00:00
According to your query :
$currendate have date
and $currenttime have time.
But your column name "play_datetime" suggest that it is having datetime or timestamp .
so if this is right that your query is working but you have to put $currentdatetime instead of $currenttime.
$query = "SELECT URL
FROM Matches
WHERE match_date = '" . $currentdate . "'
AND play_datetime < '" . <timestamp> . "'
AND DATE_ADD(play_datetime, INTERVAL 3 HOUR) > '" . <timstamp> . "'";
When someone visits X page for the first time, I insert a new row into the table with the current unix time()stamp.
I want to insert new rows, for that user, every 24 hours.. so for example:
Example A) Bob, goes to my site, it inserts a row.. 12 hours later, Bob comes back, it doesn't insert a new row as 24 hours haven't passed yet.
Example B) Bob, goes to my site, it inserts a row.. 24 hours later, Bob comes back, it DOES insert a new row as 24 hours HAVE passed.
I am toying around with this, but cannot think if this is right or not due to my brain being fried.
$time = time();
$difference = 86400;
$timedifference = $time + $difference;
When inserting the row:
mysql_query("INSERT INTO `logs` (`time`, `who`, `site`, `type`)
VALUES('" . $timedifference . "', '" . $ip . "', '" . $rid . "', 'out') ")
or die(mysql_error());
When checking to see if it has been 24 hours or more:
mysql_query("SELECT * FROM `logs`
WHERE `time` < '" . time() . "' AND `type` = 'out'
AND `site` = '" . $rid . "' AND `who` = '" . $ip . "'");
Can somebody please tell me if it's right?
Here is what I've come up with.. it seems to work:
//log check
$ip = ip2long($_SERVER['REMOTE_ADDR']);
$time = time(); //current time
$difference = 86400; //one day in seconds
$timedifference = $time + $difference; //time difference
$logQ = mysql_query("SELECT * FROM `logs` WHERE `time` > '" . time() .
"' AND `type` = 'out' AND `site` = '" . $id .
"' AND `who` = '" . $ip . "'");
$logR = mysql_num_rows($logQ);
if ($logR <= 0){
mysql_query("INSERT INTO `logs` (`time`, `who`, `site`, `type`) VALUES('" .
$timedifference . "', '" . $ip . "', '" . $id . "', 'out') ") or
die(mysql_error());
}
Try
insert ignore into logs
select unix_timestamp(now()), who, site, type
from logs
where
who='{$ip}' and
site='{$rid}' and
type='out' and
unix_timestamp(time)<=unix_timestamp(now())-86400 limit 1;
And check if there a return affected_rows,
if so, meaning the new log added.
I would insert $time, rather than $timedifference.
You need to check to see whether time is less than time() - 86400. If you made time a datetime column, you could do this directly in the query.
In your last query you are not checking whether there is an entry over 24 hours old, you are only checking if there's an entry that is older than NOW.
Correct procedure is:
Create an index on the records for the login time.
SELECT the last record by this ascending index, for a user ('who').
If there is a last, and last is less than 24 hours away from now (time()), then skip creation of a new record.
Otherwise, create one for now (time()).