MySQL, Why doesn't my update query work? - mysql

I'm made a copy of my MySQL database in MS Access as I was sure my query would work.
Heres my query
UPDATE Pads RIGHT JOIN Fix ON Pads.PadID = Fix.PadID
SET Pads.RemoveMeDate = '1999-01-01 00:00:00'
This query work in MS Access, but not in MySQL.
How do I fix this ? and why doesn't it work ?
EDIT
* When I say my query doesn't work, I mean no rows affected, when there are matching records ... *

I don't see a need for that join?
Try something like this:
UPDATE Pads
SET Pads.RemoveMeDate = '1999-01-01 00:00:00'
WHERE Pards.PadId IN (
SELECT PadId FROM Fix
)

UPDATE Pads, Fix
SET Pads.RemoveMeDate = '1999-01-01 00:00:00'
WHERE Pads.PadID = Fix.PadID
or solution above / below from Nanne depending what is a reason for JOIN

Try putting Pads.PadID = Fix.PadID in parenthesis
(Pads.PadID = Fix.PadID)
I've never actually tried doing a join on an update query, so I'm not sure if that will work.

Related

Error Code 1242: Subquery retuens more than 1 row

I'm working on an update statement but I keep getting this error. Anyone have any advice on how to fix it. I've tried looking at solutions from similar questions for the past hour but can't seem to get them to work. Here's my sql statemtent:
UPDATE T_SUBSCRIBERS
SET FULLNAME=
(SELECT CONCAT (T_REGISTERED_FNAME, T_REGISTERED_LNAME) FROM T_REGISTERED WHERE
T_REGISTERED_UID = T_SUBSCRIBERS.T_SUBSCRIBERS_UID);
** Update ur sql like this :**
UPDATE T_SUBSCRIBERS
SET FULLNAME=
(SELECT CONCAT (T_REGISTERED_FNAME, T_REGISTERED_LNAME) FROM T_REGISTERED WHERE
T_REGISTERED_UID = T_SUBSCRIBERS.T_SUBSCRIBERS_UID AND ROWNUM = 1);
You have more more rows that match the conditions than you expect.
You can find the offending rows by doing:
select T_REGISTERED_UID, count(*)
from T_REGISTERED
group by T_REGISTERED_UID
having count(*) > 1;
If you just want a quick-and-dirty solution, use limit:
UPDATE T_SUBSCRIBERS s
SET FULLNAME = (SELECT CONCAT(T_REGISTERED_FNAME, T_REGISTERED_LNAME)
FROM T_REGISTERED r
WHERE r.T_REGISTERED_UID = s.T_SUBSCRIBERS_UID
LIMIT 1
);
In general, though, it is best not repeat column values like this in different tables. When you want the full name, just join to T_REGISTERED. After all, what happens if the user updates their registration name?

Update Query taking long to run

I need some assistance with a query I am using (MySQL) to update another table. When running the nested query, the query runs in less than a second. But as soon as I include the update part, it takes hours to run. The query I am using is as per the below:
UPDATE sys_reference.outlet_reference OUTREF LEFT JOIN
(SELECT
store_code 'storeCode'
, LEFT(header_value,20) 'CoOrds'
FROM
am_data_warehouse.am_headers
WHERE
action_date = CURDATE()- 1
AND header_field_id IN (3641, 4937)
) GPSCO
ON OUTREF.store_code = GPSCO.storeCode
SET OUTREF.gps_coordinates = GPSCO.CoOrds
Below is the structure of the table that is being updated:
I think the sub-query isn't doing you any favors here. I think you can rewrite it to elimiate the sub-query.
UPDATE
sys_reference.outlet_reference AS OUTREF
INNER JOIN am_data_warehouse.am_headers AS GPSCO ON OUTREF.store_code =
GPSCO.storeCode
SET
OUTREF.gps_coordinates = LEFT(GPSCO.header_value,20)
WHERE
GPSCO.action_date = CURDATE() - 1
AND GPSCO.header_field_id IN (3641, 4937)

MySql Update where table_A.col_1 Between table_B.col_1 and table_B.col_2

can you help me with an sql query?
I want to get a value from another table but cannot use an inner join because the "join" column is not exactly the same in these two tables. Instead I must check the place where "Table_A.clubZipCode BETWEEN Table_B.zip_min AND Table_B.zip_max" holds.
This is my research efford so far:
UPDATE
Table_A
SET
Table_A.clubState = Table_B.state
FROM
clubs_data AS Table_A
JOIN zip_to_state AS Table_B
WHERE
Table_A.clubZipCode BETWEEN Table_B.zip_min AND Table_B.zip_max
However it draws an syntax-error on line 5.
Thank you!
Here is your query re-written with a valid syntax:
UPDATE clubs_data
SET clubState = (
SELECT state
FROM zip_to_state
WHERE clubs_data.clubZipCode >= zip_to_state.zip_min AND clubZipCode <= zip_to_state.zip_max
LIMIT 1
);
I do not know what you want to achieve, but generally the update syntax using JOIN would be something like this:
UPDATE clubs_data AS Table_A
JOIN zip_to_state AS Table_B
ON Table_A.clubZipCode >= Table_B.zip_min
AND Table_A.clubZipCode <= Table_B.zip_max
SET Table_A.clubState = Table_B.state;
EDIT: The solution just proposed by #Gab using a subquery might be better suited for you in this case.

how to update one table using data from second table

I am bit stuck with this one.. what i want is update app_name (first table). It returns no error.. but does nothing...
UPDATE tbl_m_app AS tma, tbl_measure AS tm
SET tma.app_name='Ap1'
WHERE (tm.mesure_id = tma.mesure_id
AND tm.live = 1)
This query will do the same work in more obvious way and without joins
UPDATE tbl_m_app AS tma
SET tma.app_name='Ap1'
WHERE tma.mesure_id IN (SELECT tm.mesure_id FROM tbl_measure AS tm WHERE tm.live = 1)
I think this SQL is fine, it's just not matching any rows.
Check this by using a select with the same where clause:
SELECT * FROM tbl_measure tm WHERE tm.live=1;
0 rows returned, right?

MySQL update from one table to another with condition not working?

I have tried a solution that seems to work for someone else on this question:
Update table a from table b where (conditions)
I can not seem to get it working, MySql gives me a syntax error.
I have two tables, and I need to update a column in one table to the value of another column where an id matches in both tables.
UPDATE video_data SET video_data.date_timestamp = video.date_timestamp FROM video_data JOIN video ON video_data.video_id = video.video_id
I am not sure what the issue is with my syntax. I am quite tired and maybe it is just my eyes playing with me. Thanks for the help!
Try this syntax out:
UPDATE video_data, video
SET video_data.date_timestamp = video.date_timestamp
WHERE video_data.video_id = video.video_id
UPDATE video_data, video SET video_data.date_timestamp = video.date_timestamp
WHERE video_data.video_id = video.video_id
Try this:
UPDATE video_data SET date_timestamp = (SELECT video.date_timestamp FROM video WHERE video.video_id = video_data.video_id)
Although the error in your actual query is simply that you missed a "SELECT"
UPDATE video_data SET video_data.date_timestamp = SELECT video.date_timestamp FROM video_data JOIN video ON video_data.video_id = video.video_id
But I don't think it is what you want it to be.