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;
Related
Here is the issue to solve. I have a table USERS and a table GROUP_USER. I am updating table USERS and the data I update in that table will directly affect the GROUP_USER table.
Here are the tables
**Users table**
id
number_of_items
group_type //**group_type** value from GROUP_USER table
current_group //id column value from GROUP_USER table
**Group_user table**
id
group_type
item_count
//Query to update users table
"UPDATE users SET number_of_items = number_of_items - 1 WHERE item_type = :item AND number_of_items > 0"
What I want is...IF after subtracting (1) item from number_of_items column in USERS table, the remainder equals zero(0), then subtract a value of (1) from the 'item_count' column in the GROUP_USER table WHERE group_type in GROUP_USER table is equal to current_group in USERS table. So that for every number_of_items that reaches zero, it will subtract 1 from the Group_user table based off of which group_type they are a part of.
a query similar to this, if even possible:
UPDATE
users a
SET
a.number_of_items = a.number_of_items - 1
WHERE
a.item_type = :item AND a.number_of_items > 0
(
if a.number_of_items - 1 = 0
UPDATE
group_user b, users a
SET
b.item_count - 1
WHERE
b.id = a.current_group
)
I'll probably have to run the next query separate, but it will update all users number_of_items to 0 if current_group is 0. Sorry, its a bit complicated.
UPDATE
users
SET
current_group = 0
WHERE
number_of_items = 0
Use a LEFT join of the tables in the UPDATE statement:
UPDATE users u
LEFT JOIN group_user g
ON g.id = u.current_group AND u.number_of_items = 1
SET u.number_of_items = u.number_of_items - 1,
g.item_count = g.item_count - 1
WHERE u.item_type = :item AND u.number_of_items > 0;
Maybe the conditions in the ON clause of the join need to include the columns group_type also:
ON g.id = u.current_group AND g.group_type = u.group_type AND u.number_of_items = 1
If you are updating only one record in users table then you can use variables. Your query will be :
SET #ID = 0;
SET #C = 0;
UPDATE
users a
SET
a.number_of_items = #C := a.number_of_items - 1,
current_group = #ID := current_group
WHERE
a.item_type = :item AND a.number_of_items > 0
;
UPDATE
group_user
SET
item_count - 1
WHERE
(#C = 0) AND (id = #ID)
;
I used two variables #ID, #C these variables will hold the values from users table then I used them in the second query. Note that the second query will be executed only when #C=0 .
I don't want to have variables in the query. How should I do it?
Below is my MYSQL query:
SET #totalcnt:=(
select prof.failed_attempt_count
from profile prof, app_user usr
where upper(usr.username) = upper("admin") and
prof.id = usr.profile_id and
usr.enterprise_id = (select id
from enterprise
where upper(enterprise_code) = upper("e100") ));
update app_user
set cur_failed_attempt_count = cur_failed_attempt_count + 1
where (upper(username) = upper("admin") and
enterprise_id = (select id
from enterprise
where upper(enterprise_code) = upper("e100")) and
cur_failed_attempt_count < #totalcnt
);
Basically I want to get rid of #totalcnt and first SET command.
Please help.
did you try to add directly #totalcnt expression into your main query?
EDIT: wrapped around LIMIT 1 since it was searched the number of affected rows by the SELECT, ergo the count of lines
update app_user
set cur_failed_attempt_count = cur_failed_attempt_count + 1
where (upper(username) = upper("admin")
and enterprise_id = (select id
from enterprise
where upper (enterprise_code) = upper("e100"))
and cur_failed_attempt_count < (select prof.failed_attempt_count
from profile prof, app_user usr
where upper(usr.username) = upper("admin")
and prof.id = usr.profile_id
and usr.enterprise_id = (select id
from enterprise
where upper(enterprise_code) = upper("e100") LIMIT 1))
);
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;
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 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.