Why isn't this bulk mysql case statement working? - mysql

UPDATE campaigns.list_name_counter SET counter = CASE WHEN name = 'occupant' THEN '2' WHEN name = 'occupant' THEN '3' WHEN name = 'Resident' THEN '3' WHEN name = 'Resident' THEN '4' WHEN name = 'Resident' THEN '5' END WHERE name IN ('occupant', 'occupant', 'Resident', 'Resident', 'Resident');
This table only has 3 columns. id, name, and counter. When it's done updating with the statement above, it only did the first occupant and the first Resident. It skipped the rest. If anything, if it only can do once per unique name, I would prefer it do the last of each, not the first of each, but anyway, should it update on each one, even if a name is repeated, or is that not allowed? and how can i make it update on the biggest counter number, not the smallest? Is my syntax messed up somewhere?

CASE statements do not "fall through" like they do in C; only the first match will be used; they are equivalent to "if...else if....else if...else if...."

Related

Combine 2 update querys in a single query - performance

Im programming a favourite function.
For example we have multiple adresses and can choose one as favourite.
At the moment i got 2 querys to do this job:
UPDATE userdata
SET maindata = 0
WHERE
cid = :id;
UPDATE userdata
SET maindata = 1
WHERE
cid = :id AND id = :id2
LIMIT 1
In the first query i make all adresses as "no favourite" and in the second one i make the new choosen adress the favourite one.
Is there any way to imrpove this query or rewrite both into 1 ? Or even a better solution ?
If you want a single query you could use a case when (or an if)
update userdata
set maindate = case when id = :id2 then 1
else 0 end
where cid = :id;
for performance Be sure you have a proper index on userdata columns (cid, id)
and the number of rows scanned should be the same for the first wuary ..but in this way you avoid the second ..
eventually try create a composite index
create index myidx1 on userdata(cid, id)
UPDATE userdata SET maindata = (case when cid = id AND id = id2 then 1 else 0 end);
This will help. I am not sure of your query but this will help. let me know if you looking something different...

Update column based on value in another table without joins

Up front, I'm in a DB class and could use a hint to get closer to the correct answer.
In the ticket_old table there is the first and last name of technicians. Only two unique names.
In the new ticket table, I've got a tech_id column which needs the int matching the last_name of the tech found in the ticket_old table.
I've been trying to do this using the code below, which executes successfully and updates 0 rows.
UPDATE ticket,ticket_old
SET tech_id = (CASE WHEN ticket_old.techLast = 'name1' THEN 1
WHEN ticket_old.techLast = 'name2' THEN 2
END)
;
-edit, I also tried the following which runs and updates 0 rows.
UPDATE ticket,
(SELECT techLast FROM ticket_old WHERE techLast = 'name1') as src
SET ticket.tech_id = 1;
When Comparing two values,
Always use Double Equal marks:
SET tech_id = (CASE WHEN ticket_old.techLast == 'name1' THEN 1
WHEN ticket_old.techLast == 'name2' THEN 2
END)
I'm not sure if it is 'name' or "name". Try it.

I need to compare two rows of data for duplicate

I need to compare two rows of data for duplicate, if the consecutive rows are the same activityID I need to notate one is in error. This code is not working as I wish
My code is not testing correctly.
SELECT
CASE
WHEN activityId = activityId +1 THEN activityId = 'error'
ELSE activityId = activityId
END AS sortOrder,recordId,
activityId,
started,
completed,
userId
FROM log1644
You can not access the next or previous row like that. To be exact, there actually is not really a next or previous row, meaning that in a database data has no specific order when you don't specify it. As long as you don't have an ORDER BY one_or_multiple_columns in your query, the result you get back might get displayed in another order each time you issue your query.
That said, you have two possibilities:
join the table to itself
use variables
Joining the table to itself would work something like this
SELECT * FROM table alias_a
INNER JOIN table alias_b ON a.id = b.id + 1
Then a CASE WHEN a.whatever = b.whatever THEN 'error' ELSE 'cool' END would work, but this can become ugly pretty quickly, for example when the next id is not +1 but sometimes +2 because you deleted a row sometime or other causes. Or performancewise this can be bad, anyway...
I prefer using variables for cases like this.
SELECT
yt.*,
CASE WHEN #prevID = id THEN 'oops, same id as row before' ELSE 'another_id' END AS is_it_different,
#prevID := id
FROM your_table yt,
(SELECT #prevID := 0) table_alias__variable_initialization
ORDER BY id, another_column_determining_the_order_of_ids
As mentioned earlier, the ORDER BY is important here.
In the CASE WHEN ... the variable #prevID still has the value of the previous row. In the next line of the statement, the value of the current row gets assigned to the variable.

SQL server: where clause for optional fields

Track Id is optional field in my application.
Case 1: If I pass valid track id, it should return the respective rows.
Case 2: If invalid track id is passed, no rows should be returned.
Case 3: If no track id passed, all rows should be returned
DECLARE #transaction_ID INT
SELECT #transaction_ID = Transaction_ID FROM myTable WHERE TRACK_ID= #Track_Id
My where condition is:
WHERE (#transaction_ID IS NULL OR myTable.Transaction_ID = #transaction_ID)
AND (amount<>0)
with the above condition 1 and 3 cases are working fine. but 2nd case got failed. When I passed invalid track id, all rows are getting returned. Please correct the query to handle the case 2. Thanks in advance.
Just continue to query #Track_ID as well:
WHERE (
(#transaction_ID IS NULL AND #Track_Id IS NULL) OR
myTable.Transaction_ID = #transaction_ID)
AND (amount<>0)
(The only situation where you want a NULL #Transaction_ID to make this WHERE clause succeed is case 3. In Case 2, a non-null #Track_Id was passed but #Transaction_ID will be NULL because no rows were returned, so that's the situation we're trying to deal with)
Try something like this
Where 1=(Case when #TrackId = 1 Or TRACK_ID= #Track_Id then 1 else 0 end )
When you want all the data then pass 1 ,
When want data as per TrackID pass #trackID value and
when you dont wnat the condition to be applied pass null

mysql update multiple rows, each with its own values, with a CASE statement

I'm trying to update two fields of several rows at once but I can't determine the right syntax to do so, except for doing so with one field update.
Each row is identified by an id, and therefore I'm using a CASE statement.
I have this table:
tbl_accounts(id_account, nation_id,
group_id)
Now, the following query works for updating only one field:
UPDATE tbl_accounts SET nation_id = CASE id_account
WHEN 3 THEN 333
WHEN 5 THEN 555
ELSE nation_id END
The above will update the nation_id field of each corresponding row identified by its id_account.
And the following query doesn't work for updating two fields - please suggest a fix to the syntax. I'm trying to avoid using any SELECT/JOIN/etc':
UPDATE tbl_accounts SET nation_id = CASE id_account, group_id = CASE id_account
WHEN 3 THEN 3331, 3332
WHEN 5 THEN 5551, 5552
ELSE nation_id, group_id END
I could run this as two separate statements but I'm sure there's a way to combine the two into one.
Any help is highly appriciated!
It sounds like you are looking for something like this:
UPDATE tbl_accounts
SET nation_id =
CASE id_account
WHEN 3 THEN 3331
WHEN 5 THEN 5551
ELSE nation_id
END,
group_id =
CASE id_account
WHEN 3 THEN 3332
WHEN 5 THEN 5552
ELSE group_id
END
But doing separate updates is a sensible solution in this situation. The above query will require checking every row in the table to see if it matches the condition. If you have an index on id_account (and presumably you do as it appears to be the primary key) then it will be very fast to update a single row.
UPDATE tbl_accounts SET nation_id = 3331, groupid = 3332 WHERE id_account = 3
UPDATE tbl_accounts SET nation_id = 5551, groupid = 5552 WHERE id_account = 5