How to get rid off the SET? - mysql

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))
);

Related

MySQL: Changing trigger definition based on the SET columns within an INSERT

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;

MySQL Database Error: Subquery returns more than 1 row

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 = ''

MySQL UPDATE field where count on another field in the same table = 1

Please could someone give me the correct syntax for below.
MySQL UPDATE tblcontact SET MainContact = 1
WHERE COUNT(tblcontact.CompanyID) = 1
GROUP BY tblcontact.CompanyID
I get the idea. You want to set the field to 1 where there is only one record. Try this:
UPDATE tblcontact c join
(select CompanyID, count(CompanyID) as cnt
from tblcontact
group by CompanyId
) cc
on c.CompanyId = cc.CompanyId and cnt = 1
SET c.MainContact = 1 ;

How to update fields form a select statement in MySql with a WHERE clause?

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.

MySQL Update Column +1?

I was wondering what would be the easiest way to update a column by +1? I will be updating a post count of a category based on when users submits a new post.
Thanks.
The easiest way is to not store the count, relying on the COUNT aggregate function to reflect the value as it is in the database:
SELECT c.category_name,
COUNT(p.post_id) AS num_posts
FROM CATEGORY c
LEFT JOIN POSTS p ON p.category_id = c.category_id
You can create a view to house the query mentioned above, so you can query the view just like you would a table...
But if you're set on storing the number, use:
UPDATE CATEGORY
SET count = count + 1
WHERE category_id = ?
..replacing "?" with the appropriate value.
You can do:
UPDATE categories SET posts = posts + 1 WHERE category_id = 42;
How about:
update table
set columnname = columnname + 1
where id = <some id>
update post set count = count + 1 where id = 101
update table_name set field1 = field1 + 1;
How to update order column Count value
Try
UPDATE order SET
Order_Count = Order_Count + 100
WHERE
Order_ID = '1234'
update TABLENAME
set COLUMNNAME = COLUMNNAME + 1
where id = 'YOURID'