I have this issue and I have investigated more than five house but find nothing :( . I have table called support.
UPDATE support s SET s.Survey_Status = 0
CASE
WHEN s.Survey_Status = 0 Then 1
WHEN s.Survey_Status = 1 Then 2
End
Where last_response < ADDDATE(CURDATE(), INTERVAL 1 DAY)
and support_waiting ="n" ;
I need to update the support table and set the survey_status =1 except the fifth row in the table will be =2 . For example, if I have survey_ status from 1 to 10 = 1 then the fifth will =2 . any idea please ??
By the way, I am working with mysql Heidi .
Thanks in Advance
You can combine user variables and MOD():
UPDATE support, (SELECT #r:=0) init
SET Survey_Status = IF(MOD(#r:=#r+1,5), 1, 2)
WHERE last_response < CURRENT_DATE + INTERVAL 1 DAY
AND support_waiting = 'n'
ORDER BY ...
Related
I am new with MYSQL and I am having trouble with this code:
UPDATE gf_order_history
SET date_add =
( SELECT delivery_date
FROM gf_orders
WHERE gf_order_history.id_order = gf_orders.id_order
AND gf_order_history.id_order_state = 4);
When I execute this code all the fields in id_order_state = 4 has been well updated but all the rest id_order_state which are different from 4 has been replaced to 0 .
Do you have any ideas what did I do wrong ?
All I want is the field id_order_state = 4 only to be updated.
Thank you very much for your help,
Sincerely,
Cyril
You forgot a where in your update query
UPDATE gf_order_history
SET date_add =
( SELECT delivery_date
FROM gf_orders
WHERE gf_order_history.id_order = gf_orders.id_order )
WHERE gf_order_history.id_order_state = 4
I would like to update each DATE field and add him 7 days. i check for DATE functions in php but i didn't found function that fit to MySQL syntax
my query is:
$query_sched = "UPDATE events SET events.event_date = DATE(events.event_date + INTERVAL 7)";
$res = mysqli_query($dblink, $query_sched) or die (mysqli_error($dblink));
You are missing a '+'
UPDATE events SET events.event_date = DATE(events.event_date + interval 7 day) WHERE events.event_id = '$event_id'
UPDATE events SET events.event_date = DATE_ADD(events.event_date , interval 7 day) WHERE events.event_id = '$event_id'
Output:
update test set test.date = date(test.date + interval 7 day) where test.id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
I've got a rather large query that is trying to get a list of carriers and compare the amount of insurance they have on record to identify carriers that do not meet a minimum threshold. If I run the select query it works just fine with no errors. But when I try to use it for an insert into a table it returns this error message
[Err] 1366 - Incorrect decimal value: '' for column '' at row -1
I have to use the cast as decimal at the bottom of this query because the value that is being stored in the database is a varchar and I cannot change that.
Anyone have any ideas?
set #cw_days = 15;
INSERT INTO carrier_dnl (carrier_id, dnl_reason_id, status_id)
SELECT work_cw_carrier_status_update.carrier_id, company_dnl_schema.dnl_reason_id,
CASE
WHEN work_cw_carrier_status_update.comparison_date > #cw_days THEN 1
ELSE 4
END as status
FROM work_cw_carrier_status_update
JOIN company_dnl_schema
ON company_dnl_schema.dnl_reason_id = 51
LEFT OUTER JOIN carrier_insurance
ON carrier_insurance.carrier_id = work_cw_carrier_status_update.carrier_id
WHERE ifnull(carrier_insurance.insurance_type_id,4) = 4
AND date(now()) BETWEEN IFNULL(carrier_insurance.insurance_effective_date,DATE_SUB(now(),INTERVAL 1 day)) AND IFNULL(carrier_insurance.insurance_expiration_date,DATE_ADD(now(),INTERVAL 1 day))
AND CASE WHEN NULLIF(carrier_insurance.insurance_bipdto_amount,'') is null THEN 0 < company_dnl_schema.value
ELSE
ifnull(cast(replace(carrier_insurance.insurance_bipdto_amount, '*','') as decimal),0) < company_dnl_schema.value
END
AND ( work_cw_carrier_status_update.b_bulk = 0 OR work_cw_carrier_status_update.b_bulk = 1 )
AND ( work_cw_carrier_status_update.b_otr = 1 OR work_cw_carrier_status_update.b_ltl = 1
OR work_cw_carrier_status_update.b_dray = 1 OR work_cw_carrier_status_update.b_rail = 1
OR work_cw_carrier_status_update.b_intermodal = 1 OR work_cw_carrier_status_update.b_forwarder = 1
OR work_cw_carrier_status_update.b_broker = 1 )
group by work_cw_carrier_status_update.carrier_id;`
If the select seems to work, then there are two possible problems. The first is that the select doesn't really work and the problem appears further down in the data. Returning one or a handful of rows is not always the same as "working".
The second is an incompatibility with the types for the insert. You can try to use silent conversion to convert the values in the select to numbers:
SELECT work_cw_carrier_status_update.carrier_id + 0, company_dnl_schema.dnl_reason_id + 0,
(CASE WHEN work_cw_carrier_status_update.comparison_date > #cw_days THEN 1
ELSE 4
END) as status
This may look ugly, but it is not nearly as ugly as storing ids as strings in one table and as numbers in another.
Hello I tried to update a row in table if somebody is offline more than 1 minute, and set LoggedIn then as 0, but my SQL query doesn't work, what I do wrong?
"UPDATE acc SET LoggedIn='0' WHERE LastTimeActive<(NOW(), INTERVAL 1 MINUTE)";
You are missing date_sub():
UPDATE acc
SET LoggedIn = '0'
WHERE LastTimeActive < date_sub(NOW(), INTERVAL 1 MINUTE);
Can someone please tell me what is wrong with my query.
I am trying to fetch data fro my table based on the column called weekend, if weekend is set to "0" show from Sunday 6pm until Friday 9pm, then if weekend is set to "1" Show from Friday 9pm until Sunday 6pm.
SELECT *
FROM closures
WHERE closures.weekend = 0
OR WEEKDAY(NOW()) < 4
AND closures.weekend = 1
OR WEEKDAY(NOW()) > 4
OR (WEEKDAY(NOW())=4 AND HOUR(NOW())>21)
OR (WEEKDAY(NOW())=6 AND HOUR(NOW())<18)
It helps to phrase the question properly. What you should have said is "between 9pm Friday and 6pm Sunday I want to show the rows where closures.weekend = 1, otherwise show those where closures.weekend = 0".
Hence what you need to do is generate a value of 1 or 0 depending on whether it's the weekend or not, and then SELECT those rows where weekend has that value, i.e:
SELECT *
FROM
closures
WHERE
weekend = IF(
(WEEKDAY(NOW()) = 4 AND HOUR(NOW()) >= 21)
OR (WEEKDAY(NOW()) = 5)
OR (WEEKDAY(NOW()) = 6 AND HOUR(NOW()) < 18)
, 1, 0)
Weekend cant be both, 0 and 1
SELECT *
FROM
closures
WHERE
closures.weekend = 0
OR
(
WEEKDAY(NOW()) < 4
AND closures.weekend = 1
)
OR WEEKDAY(NOW()) > 4
OR
(WEEKDAY(NOW())=4 AND HOUR(NOW())>21)
OR
(WEEKDAY(NOW())=6 AND HOUR(NOW())<18)
My first guess is that the
SELECT field1, field2, ...
is missing ?