MySQL Update Column +1? - mysql

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'

Related

Using HAVING in UPDATE with WHERE CLAUSE

i am trying to update a table by setting column VAT TO 2 where we have duplicate values in column image
SET
VAT = 2
WHERE id >0
HAVING count(image) > 1
From your comment to a previous answer I assume that you use MySql.
In MySql you need to join the table to a query that returns the duplicate images:
update tablename t inner join (
select image
from tablename
where id > 0
group by image
having count(*) > 1
) i on i.image = t.image
set vat = 2;
You may use this.
For SQL Server
update t set t.VAT = 2 from applicantinfo as t inner join
(select Image from applicantinfo group by image having count(*)>1) as b
on t.image = b.image
You could do this:
UPDATE applicantinfo
SET VAT = 2
WHERE image IN (
SELECT image
FROM (SELECT * FROM applicantinfo)
WHERE id > 0
GROUP BY image
HAVING COUNT(*) > 1
)
SELECT inside the WHERE clause supplies duplicate images of rows with ids above zero.

Updating table column based on other table column

I want to update table cases_proceedings but it will check some criteria for this in the other table cases_attach_files i.e.
SELECT cp.sno FROM cases_proceedings cp, cases_attach_files cf
WHERE cp.`case_sno` = cf.`case_sno` AND cf.page_lbl_sno = 10 AND
cp.`is_decided` = 0
But when i want to update the first table value and search the sno of the first table by using the IN operation with subquery like this:
UPDATE cases_proceedings
SET `is_decided` = 1
WHERE sno IN(SELECT cp.sno FROM cases_proceedings cp, cases_attach_files cf
WHERE cp.`case_sno` = cf.`case_sno` AND cf.page_lbl_sno = 10 AND
cp.`is_decided` = 0);
It shows an error:
Error Code: 1093
You can't specify target table 'cases_proceedings' for update in FROM clause
i don't know how to fix this?
One workaround here is to wrap the subquery in another subquery:
UPDATE cases_proceedings
SET is_decided = 1
WHERE sno IN
(
SELECT sno
FROM
(
SELECT cp.sno
FROM cases_proceedings cp
INNER JOIN cases_attach_files cf
ON cp.case_sno = cf.case_sno AND
cf.page_lbl_sno = 10 AND
cp.is_decided = 0
) t
);
The extra subquery works around the error because it forced MySQL to materialize the subquery containing the joins.
According to documentation, In MySQL, you can't modify the same table which you use in the SELECT part.
You can either wrap it in subquery or joining both tables as shown below:
UPDATE cases_proceedings cp
INNER JOIN cases_attach_files cf
ON cp.case_sno = cf.case_sno
AND cf.page_lbl_sno = 10
AND cp.is_decided = 0
SET cp.is_decided = 1

return rows with specific tag

I have 3 tables
listings
listtags
listing_listtag
listing can have multiple tags assigned to it and those are stored in listing_listtag with forein keys to listings and listtags table
I have a problem to select only a listing which has 2 tags associated with it.
I have this query:
select *
from `listtags`
inner join `listing_listtag` on `listtags`.`id` = `listing_listtag`.`tag_id`
where `listing_listtag`.`listing_id` = 5
and `slug` = "delivery"
AND slug = "wireless-internet"
and it returns nothing. if I chnage the last AND to OR it now returns
I would like the query to only return one row where listing_id is 5, how can I do this?
Here is one method:
select ?.listing_id
from listtags lt inner join
listing_listtag llt
on lt.id = llt.tag_id
where llt.listing_id = 5 and
?.slug in ('delivery', 'wireless-internet')
group by ?.listing_id
having count(*) = 2;
The ? is for the table alias of the table with the column.
Note: If the data can have duplicates, use count(distinct slug).
You just need a pair of brackets:
select *
from `listtags`
inner join `listing_listtag` on `listtags`.`id` = `listing_listtag`.`tag_id`
where `listing_listtag`.`listing_id` = 5
and (`slug` = "delivery"
or slug = "wireless-internet")

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.