I have two table in my SQL Database
One is Companies & Another is Company_advertisements.
I want to update field of company table "adv"
SET Company.adv = 1 if Company_adv.image is not empty
SET Company.adv = 0 if Company_adv.image is empty
The foreign key for Company_adv table is company_id
I tried using the following syntax but it didn't worked.Instead after running this query all the values of Companmy.adv become 1 after that
I tried this query:
UPDATE companies cmp, company_adv cma
SET cmp.adv=1
WHERE cma.company_id=cmp.id AND
cma.image1 IS NOT NULL
Please help me !!
if that's the case, you need to use LEFT JOIN
UPDATE companies cmp
LEFT JOIN company_adv cma
ON cmp.id = cma.company_id
SET cmp.adv = IF(cma.image1 IS NULL, 0, 1)
You could try:
UPDATE companies
SET adv=IF((SELECT image
FROM company_adv
WHERE companies.id=company_adv.company_id) IS NULL,
0,
1);
Related
I am trying this query built from findings in various guides and other stackoverflow questions:
UPDATE `characters`
SET `level`, `xp`, `taximask` = (SELECT `level`, `xp`, `taximask`
FROM `characters`
WHERE `guid` = 111) WHERE `name` = 'targetname';
In this example i want to copy the contents of the level, xp and taximask columns from the row where the guid matches 111 to the row where name matches targetname. Both in the same db/table.
Since the 3 columns are just placeholders for a query where i need to copy 25+ columns, i am looking for a way to make the query work and possibly shorten it.
Here is the working solution i got from a friend:
UPDATE `characters` AS t1
INNER JOIN `characters` AS t2 ON t2.guid = 111
SET t1.level = t2.level, t1.xp = t2.xp, t1.taximask = t2.taximask
WHERE t1.`name` = 'targetname';
I understand basically the concepts of UPDATE to use data in one table to update another similar table. However the table data I have to update to is arranged in a 'vertical' manner as opposed to the 'horizontal' manner of the input table. This query works if I limit it to just one record :
SELECT #userid:=user_id,#club:=CLUB, #financialdate:=FINANCIALDATE
FROM wpty_sa_tmp_update where user_id = 1;
UPDATE wpty_cimy_uef_data SET VALUE = #financialdate WHERE user_id = #userid and field_id = 16;
UPDATE wpty_cimy_uef_data SET VALUE = #club WHERE user_id = #userid and field_id = 8;
If I remove the WHERE user_id clause, it does not update .. what am I missing?
Obviously I can't create a join of any sort because the 2 tables don't share a common ID or key
cheers
You could actually do this from a single update statement:
UPDATE wpty_cimy_uef_data wc
INNER JOIN wpty_sa_tmp_update ws
ON wc.user_id = ws.user_id AND ws.user_id = 1
SET
VALUE = CASE field_id WHEN 16 THEN ws.FINANCIALDATE
WHEN 8 THEN ws.CLUB END
WHERE
field_id IN (8, 16);
I have the below code:
UPDATE VIEW
LEFT JOIN sem_view ON (view.semester = sem_view.semester)
SET view.t_credit = SUM(sem_view.credit)
but its not working saying invalid use of group
Data for updating must be perpared previously, in subquery. Updating must use the data which is already aggregated.
Left joining may be errorneous - it will set the value in the destination table to NULL if according data for the semester in interest in sem_view is not present. But if the logic needs this then you may use LEFT JOIN instead of INNER one in the below query.
Totally:
UPDATE `view`
JOIN ( SELECT sem_view.semester, SUM(sem_view.credit) summ
FROM sem_view
GROUP BY sem_view.semester ) data_for_updating
ON `view`.semester = data_for_updating.semester
SET `view`.t_credit = data_for_updating.summ;
PS. The text looks like view is a name of a table to be updated.
I am trying to fill in fields in a table with date of another table.
In the table 'blanko' I have a column 'product_sku' and 'virtuemart_product_id'.
In the table 'jml_virtuemart_products' I have (among others) the columns 'product_sku' and 'virtuemart_product_id'.
Now I want to add values from jml_virtuemart_products.virtuemart_product_id column into the the same column in 'blanko' from rows with where product_sku is the same.
I am trying with this query and it works partialy.
UPDATE blanko b1 SET virtuemart_product_id = (SELECT virtuemart_product_id FROM jml_virtuemart_products v1 WHEREe v1.product_sku = b1.product_sku);
The problem is that it add endless amount of rows with NULL values.
Can someone explain what I am doing wrong? I am running in circles...
Better way is to use join to update the record
update blanko b1
join jml_virtuemart_products v1 on v1.product_sku = b1.product_sku
set b1.virtuemart_product_id = v1.virtuemart_product_id
Im trying to write a formula in SQL Server to subtract the values of two columns which are [AmountSpent]column of table2 and [Amount] column of table1of two different tables and update the balance amount in [Amount] column any idea ?
Thank you in advance...
I think the following will work, assuming you have a reliable foreign key relationship between the two tables
UPDATE [table1]
SET [table1].[Amount] = [table1].[Amount] - [table2].[AmountSpent]
FROM
[table1] INNER JOIN
[table2] ON
[table1].[KeyField] = [table2].[KeyField]
i got it.........thanks #james Osborn
create procedure SP_Subtraction
(
#EmpID int
)
as
begin
UPDATE PTS_BalanceTracker
SET PTS_BalanceTracker.Balance_BalanceAmount = PTS_BalanceTracker.Balance_BalanceAmount - PTS_Transactions.Transaction_Amount
FROM
PTS_BalanceTracker INNER JOIN
PTS_Transactions ON
PTS_BalanceTracker.Emp_ID = #EmpID
end