I have a temporary table named _temp that contains orders that will be sent back to a webpage. When this table is created it populates all the information except the number of photos in each order (currently it will set the number of photos to -1 so that I know it hasn't been updated). I want to update the number of photos in each order to the correct number. I have no idea how to do this without using a while statement and going through each order individually. Here is the code I'm currently using which works fine but is very slow
SET _current_id = (SELECT old_id FROM _temp WHERE photos = -1 LIMIT 1);
WHILE (_current_id IS NOT NULL) DO
UPDATE _temp SET photos = (SELECT COUNT(f.id) FROM files f WHERE f.file_type = "orders" AND f.table_id = _current_id AND deleted = 0) WHERE old_id = _current_id;
SET _current_id = (SELECT old_id FROM _temp WHERE photos = -1 LIMIT 1);
END WHILE;
update _temp t set t.photos = (SELECT COUNT(f.id) FROM files f WHERE f.file_type = "orders" AND f.table_id = t.old_id AND deleted = 0) WHERE old_id = t.old_id;
Related
I'm aware of this:
IF EXISTS (SELECT * FROM table WHERE pk = #pk)
BEGIN
--Update
END
ELSE
BEGIN
--INSERT
END
But this works for one row. Let's say I have a table min_max M and a temp table __MinMaxImport T. I want to INSERT from T to M, but UPDATE when the pair branch_no/product already exists in M.
INSERT:
INSERT INTO min_max
(user_no, branch_no, product, min_stock, max_stock)
SELECT
#user_no, _branch_no, _product, _min_stock, _max_stock
FROM
traxs_temp..__MinMaxImport
WHERE
session_id = #session_id
UPDATE:
UPDATE min_max SET
date_time = GETDATE(),
user_no = #user_no,
min_stock = _min_stock,
max_stock = _max_stock
FROM
min_max M
INNER JOIN traxs_temp..__MinMaxImport T ON T._product = M.product
AND T._branch_no = M.branch_no
WHERE
session_id = #session_id
What is the best solution to do this? I read about MERGE but I'm not sure how to use it.
MERGE should be able to do it.
MERGE INTO min_max
USING
(
SELECT
#user_no AS _user_no, _branch_no, _product, _min_stock, _max_stock
FROM
traxs_temp..__MinMaxImport
WHERE
session_id = #session_id
) AS Src
ON
(min_max.branch_no = Src._branch_no) AND
(min_max.product = Src._product)
WHEN MATCHED THEN
UPDATE SET
date_time = GETDATE(),
user_no = Src._user_no,
min_stock = Src._min_stock,
max_stock = Src._max_stock
WHEN NOT MATCHED BY TARGET THEN
INSERT
(user_no, branch_no, product, min_stock, max_stock)
VALUES
(Src._user_no,
Src._branch_no,
Src._product,
Src._min_stock,
Src._max_stock)
;
I have two insert statements:
INSERT INTO `users votes` SET `forumtopicid` = '$id'
INSERT INTO `users votes` SET `forumtopicid` = '$forumtopicid', `replyid` = '$id'
Now here is my current trigger definition:
UPDATE `forum topics` ft
SET votes = votes + 1
WHERE NEW.forumtopicid = ft.id
I want to change it so if the INSERT contains replyid, than the UPDATE becomes:
UPDATE `forum replies` fr
SET votes = votes + 1
WHERE NEW.replyid = fr.id
You would use an if statement in the trigger:
if new.replyid is null then
UPDATE `forum topics` ft
SET votes = votes + 1
WHERE NEW.forumtopicid = ft.id;
else
UPDATE `forum replies` fr
SET votes = votes + 1
WHERE NEW.replyid = fr.id;
end if;
Please see below query, I'm stuck trying to update one table from another table that seems to have duplicates.
What will be the correct way to run this statement?
UPDATE doc SET idx2 = (SELECT imp.idx2 FROM imp WHERE imp.idx1 = doc.idx1),
idx3 = (SELECT imp.idx3 FROM imp WHERE imp.idx1 = doc.idx1)
WHERE doc.boxid IN (SELECT box.id FROM box WHERE box.profid = 41
AND box.boxname = '1153-BATCH0011') AND doc.idx2 = ''
If you're are sure the subqueries are returning duplicate same values, you can just use LIMIT 1 this way your subqueries will return 1 result only.
UPDATE doc SET idx2 = (SELECT imp.idx2 FROM imp WHERE imp.idx1 = doc.idx1 LIMIT 1),
idx3 = (SELECT imp.idx3 FROM imp WHERE imp.idx1 = doc.idx1 LIMIT 1)
WHERE doc.boxid IN (SELECT box.id FROM box WHERE box.profid = 41
AND box.boxname = '1153-BATCH0011') AND doc.idx2 = ''
I am trying to update a field from a select statement. This is my current query
UPDATE phone_cals
SET call_code_id = id, result_code_id = 0, call_subject = title WHERE status = 1
select call_code_id AS id, call_code_title AS title FROM call_codes
I am trying to set
phone_calls.call_code_id = call_codes.call_code_id
phone_calls.result_code_id = 0
phone_calls.call_subject = call_codes.call_code_title
WHERE phone_calls.status = 1
Yes I have a syntax error but I am not sure how to fix it
Summery I want to select a random call_codes.call_code_id and assign it = to phone_calls.call_code_id
If I understand your question correctly, I think you need something like this:
UPDATE
phone_calls,
(select call_code_id, call_code_title FROM call_codes ORDER BY rand() LIMIT 1) c2
SET
phone_calls.call_code_id = c2.call_code_id,
phone_calls.result_code_id = 0,
phone_calls.call_subject = c2.call_code_title
WHERE
phone_calls.call_subject = 1
This will update all call_code_id and all call_subject to a random value choosen from call_codes table, where call_subject=1.
Edit
If you need to update each row to a different random value, I think you could use this:
UPDATE
phone_calls
SET
call_code_id = (select call_code_id FROM call_codes ORDER BY rand() LIMIT 1),
result_code_id = 0
WHERE
phone_calls.call_subject = 1;
UPDATE
phone_calls INNER JOIN call_codes
ON phone_calls.call_code_id = call_codes.call_code_id
SET
phone_calls.call_subject = call_codes.call_code_title
WHERE
phone_calls.call_subject = 1;
(don't know if it's possible to do it using just one query). It can be slow, but it should work.
I wrote a query, and Im sure that it was right form.
But I get an error. :)
May I do this query else?
UPDATE pages SET
p_name = 'Activites',
p_active = 1,
p_parent = 'sport',
p_parent_id=(
SELECT p_id FROM pages WHERE p_link='sport' LIMIT 1
),
p_link = 'activites'
WHERE p_id = 9;
Thank you.
You can't do that this way. You need to cross join the table and subquery:
UPDATE
pages AS p
CROSS JOIN (
SELECT p_id FROM pages WHERE p_link='sport' LIMIT 1
) AS sq
SET
p.p_name = 'Activites',
p.p_active = 1,
p.p_parent = 'sport',
p.p.parent_id= sq.p_id
p.p_link = 'activites'
WHERE p.p_id = 9;