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
Related
Good day,
I have a question.
I have a table called event_booking.
Here we have events and each event has a separate location.
So we have event
event_booking has :
booking_id
booking_name
booking_date
booking_location
booking_comments
The type of structure is :
booking_id = integer
booking_name = varchar
booking_date = datetime
booking_location = integer
booking_comments = text
Now I want to create a Query that lists all event locations and for each location which event has been set there.
So :
I would get a result like :
booking_location = 4 & no_bookings = 256
booking_location = 7 & no_bookings = 34
booking_location = 6 & no_bookings = 128
booking_location = 3 & no_bookings = 24
Now I have fiddled a lit and created the following QUERY.
That's correct in terms of syntax. But totally wrong in terms of output.
SELECT `booking_location`, `booking_location` AS `selector`, (SELECT COUNT(1) FROM `event_booking` WHERE `booking_location` = `selector`) AS `no_bookings` FROM `event_booking` WHERE `booking_location` IN (SELECT `booking_location` FROM `event_booking` GROUP BY `booking_location`)
I know I am missing something. But what I am missing..
Thanks in advance ;-)
Assuming that you really just want to count the booking locations (based on your example of a possible result, and also your query code):
SELECT booking_location,
COUNT(booking_location) AS no_bookings
FROM event_booking
GROUP BY booking_location;
I have table named TABLE_A looks like this :
ID DATA VALUE LM
---------------------------------
1 7 9 NULL
2 10 5 NULL
3 4 7 NULL
This is not actually my table, i use this to shorten my question.
Now I want to update table_a with subquery.
This is my query :
UPDATE TABLE_A,
(SELECT VALUE AS VAL FROM TABLE_A WHERE ID = 2) AS TEMP
SET TABLE_A.LM = TABLE_A.VALUE + TEMP.VAL
WHERE TABLE_A.ID = 1
This query works on Mysql but in oracle I got error :
[Err] ORA-00971: missing SET keyword
EDIT :
This is my table [SDM_ABSENSI] :
PERIODE TGL_IN TGL_OUT IN OUT LM TL
------------------------------------------------------------------
20141011 11/01/2014 11/01/2014 08:00 17:00 NULL NULL
20141012 12/01/2014 13/01/2014 22:00 07:30 NULL NULL
20141013 13/01/2014 13/01/2014 08:00 17:00 NULL NULL
My query :
UPDATE SDM_ABSENSI A
(
SELECT PERIODE, TGL_IN, TGL_OUT, IN, OUT,
TO_DATE(TO_CHAR(TGL_IN,'YYYY-MM-DD')||' '||IN,'YYYY-MM-DD hh24:mi') AS MASUK,
TO_DATE(TO_CHAR(TGL_OUT,'YYYY-MM-DD')||' '||OUT,'YYYY-MM-DD hh24:mi') AS KELUAR
FROM SDM_ABSENSI
WHERE SUBSTR(PERIODE,0,6) = '201410'
)ABSEN
SET A.LM = (24*60) * (ABSEN.KELUAR - ABSEN.MASUK),
A.TL = CASE WHEN (24*60) * (ABSEN.KELUAR - ABSEN.MASUK) < 0
THEN 0 ELSE (24*60) * (ABSEN.KELUAR - ABSEN.MASUK)
END
WHERE SUBSTR(A.PERIODE,0,6) = '201410'
AND A.PERIODE = ABSEN.PERIODE
And i got error :
[Err] ORA-00971: missing SET keyword
Please help,
Thanks in advance
Oracle does not support Update from Join Syntax. Instead you can use Merge. Try this.
MERGE
INTO SDM_ABSENSI
USING (
SELECT PERIODE, TGL_IN, TGL_OUT, IN, OUT,
TO_DATE(To_char(TGL_IN,'YYYY-MM-DD')||' '||IN,'YYYY-MM-DD hh24:mi') AS MASUK,
TO_DATE(TO_CHAR(TGL_OUT,'YYYY-MM-DD')||' '||OUT,'YYYY-MM-DD hh24:mi') AS KELUAR
FROM SDM_ABSENSI
WHERE SUBSTR(PERIODE,0,6) = '201410'
) ABSEN
ON SDM_ABSENSI.PERIODE = ABSEN.PERIODE
WHEN MATCHED THEN
UPDATE
SET SDM_ABSENSI.LM = ( 24 * 60 ) * ( ABSEN.KELUAR - ABSEN.MASUK ),
SDM_ABSENSI.TL = CASE
WHEN ( 24 * 60 ) * ( ABSEN.KELUAR - ABSEN.MASUK ) < 0 THEN 0
ELSE ( 24 * 60 ) * ( ABSEN.KELUAR - ABSEN.MASUK )
END
I don't think you can write such a subquery in Oracle. You should maybe checkout the update statement as its defined in the oracle documentation, here http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/update_statement.htm
Having said that, what do you really want to do here? What value and under which conditions do you want to assign to the column LM?
That query doesn't look so good, in my opinion. You're trying to build a temporary table from the data stored in table_a and update that same table_a with values from that temporal table... but how? With the maximum of the temporal table? With the value of the same register when the condition is met?
I don't see how that query could work in MySQL either to be honest.
To sum up, could you provide additional info?
[EDIT] Just saw the modification in the question. You can remove the subquery from where it is and place it in the where statement...
UPDATE TABLE_A
SET TABLE_A.LM = TABLE_A.VALUE + (SELECT VALUE AS VAL FROM TABLE_A WHERE ID = 2)
WHERE TABLE_A.ID = 1
I'm learning to use mysql so i created a really simple project manager program.
i wanted to track if a user saw her/his tasks in a project. So i want to update user_seen_task filed when a project is got queried by a user, but obviously i only want to update this field if it isn't already set.
This is my approach what i have already:
UPDATE task
SET user_seen_task = 1, user_seen_task_date = NOW()
WHERE project_id = 14 AND user = 4 AND user_seen_job != 1
Unfortunately this query will update user_seen_task_date every time it runs.
Any help is greatly appreciated.
UPDATE task
SET user_seen_task = 1
, user_seen_task_date = CASE
WHEN user_seen_task_date IS NULL THEN NOW()
ELSE user_seen_task_date
END
WHERE project_id = 14
AND user = 4
AND user_seen_job != 1
Or if it doesn't need to update user_seen_task in this case, just add
AND user_seen_task_date IS NULL
into your where clause
UPDATE task
SET user_seen_task = 1, user_seen_task_date = NOW()
WHERE project_id = 14 AND user = 4 AND user_seen_job != 1
AND user_seen_task_date IS NULL
SELECT
domain_usage.url,
LEFT(url,locate('?',url)) AS cleanURL
FROM
domain_usage
WHERE
MONTH(domain_usage.login_date) = (Now()) AND
YEAR(domain_usage.login_date) = (Now());
Returns no records, and no error ?
im trying to clean the query string from URL field.....
Any thoughts ?
Your date comparisons are wrong. you're basically trying
3 = '2013-01-18 10:36:00'
you need to compare apples to apples:
MONTH(domain_usage.login_date) = MONTH(now())
modify your WHERE condtion,
SELECT domain_usage.url, LEFT(url,locate('?',url)) AS cleanURL
FROM domain_usage
WHERE MONTH(domain_usage.login_date) = MONTH(CURDATE()) AND
YEAR(domain_usage.login_date) = YEAR(CURDATE())
I'm trying to update supplier table on both PostgreSQL and MySQL using the following update statement:
UPDATE SUPPLIERS SET CURDEBT = CURDEBT + 10 WHERE ID = 5
This works fine as long as the CURDEBT column not equals null, if it is null it won't update the record. Does any body have a solution to this problem?
Thanks
In SQL, NULL is not the same thing as 0. Any operations on a NULL value still yield a NULL result. NULL + 10 is still NULL.
If you want NULL to automatically turn into "0" in this query, try this (PostgreSQL):
UPDATE SUPPLIERS SET CURDEBT = coalesce(CURDEBT, 0) + 10 WHERE ID = 5
Or MySQL:
UPDATE SUPPLIERS SET CURDEBT = ifnull(CURDEBT, 0) + 10 WHERE ID = 5
Use coalesce
UPDATE SUPPLIERS SET CURDEBT = coalesce(CURDEBT,0) + 10 WHERE ID = 5
See sqlbook
What you're looking for is COALESCE:
UPDATE SUPPLIERS
SET CURDEBT = COALESCE(CURDEBT, 0) + 10
WHERE ID = 5
Coalesce (MySQL):
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce
That behaviour is exactly what you should expect from SQL, since null + x = null, always.
You can solve it by using the COALESCE function, available in both postgres and mysql, like so:
UPDATE SUPPLIERS SET CURDEBT = COALESCE(CURDEBT,0) + 10 WHERE ID = 5