I have a table named TableX in MySQL. There are 4 columns in TableX. The columns are ColumnCompare_Now, ColumnCompare_Past, ColumnNumber_Now, ColumnNumber_Past.
I want to write a MySQL statement that has the following logic;
If ColumnCompare_Now == 'ActionNeeded' and ColumnCompare_Past == 'ActionNeeded',
then ColumnNumber_Now = `ColumnNumber_Now` + `ColumnNumber_Past`
MySQL knowledge is limited to simple Select Where statements. How can the above operation be implemented in MySQL? Meanwhile, I will try to figure this out myself while waiting for help from fellow members of StackOverflow.
I'm not completely sure I know what you're trying to accomplish. If you want to select a new column with this information, you can use a case statement:
select
case when ColumnCompare_Now = 'ActionNeeded'
and ColumnCompare_Past = 'ActionNeeded'
then ColumnNumber_Now + ColumnNumber_Past
else ColumnNumber_Now
end as ColumnNumber_Now
from tablex
And if you need this in an update query:
update tablex
set ColumnNumber_Now = ColumnNumber_Now + ColumnNumber_Past
where ColumnCompare_Now = 'ActionNeeded'
and ColumnCompare_Past = 'ActionNeeded'
update TableX set ColumnCompare_Now=(select (ColumnNumber_NOw+ColumnNumber_Past) where ColumnCompare_Now=='ActionNeeded and ColumnCompare_Past=='ActionNeeded');
kindly try out this.
Related
I'm working on an update statement but I keep getting this error. Anyone have any advice on how to fix it. I've tried looking at solutions from similar questions for the past hour but can't seem to get them to work. Here's my sql statemtent:
UPDATE T_SUBSCRIBERS
SET FULLNAME=
(SELECT CONCAT (T_REGISTERED_FNAME, T_REGISTERED_LNAME) FROM T_REGISTERED WHERE
T_REGISTERED_UID = T_SUBSCRIBERS.T_SUBSCRIBERS_UID);
** Update ur sql like this :**
UPDATE T_SUBSCRIBERS
SET FULLNAME=
(SELECT CONCAT (T_REGISTERED_FNAME, T_REGISTERED_LNAME) FROM T_REGISTERED WHERE
T_REGISTERED_UID = T_SUBSCRIBERS.T_SUBSCRIBERS_UID AND ROWNUM = 1);
You have more more rows that match the conditions than you expect.
You can find the offending rows by doing:
select T_REGISTERED_UID, count(*)
from T_REGISTERED
group by T_REGISTERED_UID
having count(*) > 1;
If you just want a quick-and-dirty solution, use limit:
UPDATE T_SUBSCRIBERS s
SET FULLNAME = (SELECT CONCAT(T_REGISTERED_FNAME, T_REGISTERED_LNAME)
FROM T_REGISTERED r
WHERE r.T_REGISTERED_UID = s.T_SUBSCRIBERS_UID
LIMIT 1
);
In general, though, it is best not repeat column values like this in different tables. When you want the full name, just join to T_REGISTERED. After all, what happens if the user updates their registration name?
I want to count points for each business for having deals or gallery :
so if the business has un empty deal, or has at least one gallery , business_data_count should be 2.
this what i've tried :
UPDATE `business` businessTable SET
business_data_count
=
sum(
(
SELECT
CASE
WHEN count(*)>= 1 then count(*)
ELSE 0
END as points
FROM gallery WHERE bussId=businessTable.bussId
)
+
(
SELECT
case
WHEN deal!='' then 1
ELSE 0
end
FROM business WHERE bussId=businessTable.bussId
)
where 1
but i got this error :
you cant specify table business for update
How to fix this ?
There's no need to do a separate select from the update table. Try this instead (untested):
UPDATE business
SET business_data_count = (deal != '')
+ (SELECT COUNT(*)
FROM gallery
WHERE bussId = business.bussId);
On a separate note, it's generally bad practice to store data that can be easily extracted with a query, such as the above.
I think you have a typo.
I think you should have:
UPDATE `business`.businessTable SET
instead of
UPDATE `business` businessTable SET
If you use the schema when defining your table in your query, you need to separate them with a dot (.).
Maybe it is not the only problem, but that's the first that comes to mind.
This is an MS Access 2010 related question.
Is it possible to go the short route (A) and write an UPDATE statement using a SELECT statement in order to catch the relevant value or do I have to go the long route (B) and I will firstly need to query the data through a SELECT statement that I will save as a query and then refer to this saved query in my UPDATE statement?
Here is (A):
UPDATE tbl_A
SET tbl_A.Header1 = (SELECT F1 FROM tblStaging
WHERE tblStaging.F1 = 'ISSUER CODE')
WHERE (((tbl_A.TableName)='tblStaging'));
Here is B:
SELECT F1
FROM tblStaging
WHERE F1 = 'ISSUER CODE';
UPDATE tbl_A, Q_A_Sel_ISSUERCODE
SET tbl_A.Header1 = [Q_A_Sel_ISSUERCODE].[F1]
WHERE (((tbl_A.TableName)='tblStaging'));
Thank you
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Chris gave the solution:
UPDATE tbl_A, tblStaging
SET tbl_A.Header1 = tblStaging.F1
WHERE (((tblStaging.F1)='ISSUER CODE') AND ((tbl_A.TableName)='tblStaging'));
I'm not sure about Access, but in SQL Server you can use a single UPDATE statement like this (and I would've thought it should also work in Access):
UPDATE A
SET Header1 = S.F1
FROM tbl_A A, tblStaging S
WHERE S.F1 = 'ISSUER CODE' AND A.TableName ='tblStaging';
Although if that's exactly what you want to do, it's the same as:
UPDATE tbl_A SET Header1 = 'ISSUER CODE'
WHERE TableName = 'tblStaging';
What I am trying to do is reduce the time needed to aggregate data by producing a roll-up table of sorts. When I insert a record, an after insert trigger is fired which will update the correct row. I would update all of the columns of the roll-up table if I need to, but since there are 25 columns in the table and each insert will only update 2 of them, I would rather be able to dynamically select the columns to update. My current update statement in the after insert trigger looks similar to this:
update peek_at_chu.organization_data_state_log odsl
inner join ( select
lookUpID as org_data_lookup,
i.interval_id,
peek_at_chu.Get_Time_Durration_In_Interval1('s', new.start_time, new.end_time, i.start_time, i.end_time) as time_in_int,
new.phone_state_id
from
(peek_at_chu.interval_info i
join peek_at_chu.interval_step int_s on i.interval_step_id = int_s.interval_step_id)) as usl on odsl.org_date_lookup_id = usl.org_data_lookup
and odsl.interval_id = usl.interval_id
set
total_seconds = total_seconds + usl.time_in_int,
case new.phone_state_id
when 2 then
available_seconds = available_seconds + time_in_int
end;
In this, lookUpID is a variable previously declared in the trigger. The field that will dictate which field of the roll-up table to update is new.phone_state_id. The phone_state_id's are not consistent, that is some numbers are skipped in this table, so an update based on column number is out the window unless I create a mapping.
The case option throws an error but I am hoping to use something similar to that instead of 25 if statements if I can.
You have to update all the columns, but use a conditional to determine whether to give it a new value or keep the old value:
set total_seconds = total_seconds + usl.time_in_int,
available_seconds = IF(new.phone_state_id = 2, available_seconds + time_in_int, available_seconds)
Repeat the pattern in the last line for all the other columns that need to be updated conditionally.
SQL:
$mysqli->query("UPDATE results
SET result_value = '".$row[0]['logo_value']."'
WHERE logo_id = '".$mysqli->real_escape_string($_GET['logo_id'])."'
AND user_id = '".$user_data[0]['user_id']."'");
This results table also contains result_tries I'd like to fetch before doing update, so I can use it to modify result_value... Is there a way to do it in a single shot instead of first doing select and than doing update?
Is this possible?
Basically:
UPDATE results SET result_value = result_value + $row[0][logo_value]
for just a simple addition. You CAN use existing fields in the record being updated as part of the update, so if you don't want just addition, there's not too many limits on what logic you can use instead of just x = x + y.