Let's say I have a table with two columns, one column is "State" and the other is "State Number".
I would need to have the column "State Number" have numeric values based on the values of the column "State", alphabetically.
example...
State = A is equals to State Number = 1
State = B is equals to State Number = 2
State = C is equals to State Number = 3
So and and so forth.
Any help would be appreciated.
Thanks!
NO, you can't do it automatically other than defining State Number to be auto_increment. What you are asking can be done using trigger but MySQL doesn't support recursive trigger and thus it's not an option here.
You can perform an UPDATE though using CASE condition and update the said column like
update tbl1
set `State Number` = case when state = 'A' then 1
when state = 'B' then 2
when state = 'C' then 3 end;
Related
Suppose I have 4 columns: A, B, C, D and one additional column: Index in a row.
Suppose I want to save a value of "50" but depending on the "Index" value of the row mysql will save 50 in either A, B, C, D. Say if Index=1 for that particular row, then 50 goes to column "A".
Is there a mysql query that will accomplish this all in one go? Or do I have to first read the index value, then make a switch statement with four different update queries to accomplish this?
Here is one way to do it:
UPDATE mytable
SET
A = CASE WHEN index = 1 THEN 50 ELSE A END,
B = CASE WHEN index = 2 THEN 50 ELSE B END,
C = CASE WHEN index = 3 THEN 50 ELSE C END,
D = CASE WHEN index = 4 THEN 50 ELSE D END
WHERE ...
The query works by doing conditional value assignment to each column, according to the value of index. When a column does not need to be updated, its original value is simply reassigned, hence turning the operation to a no-op.
If the "," in an UPDATE statement is replaced with AND, what the meaning in SQL?
Normal Update Statement :
UPDATE table
SET column1 = value ,
column2 = value
WHERE condition
My question statement :
UPDATE table
SET column1 = value AND column2 = value
WHERE condition
The affected rows seem to be different, so I want to know what my question statement mean in SQL?
Original test Data
After inserting query(from original) : Update testing set column1 = 2 , column2 =5
Normal Update
After inserting query(from original) : Update testing set column1 = 2 AND column2 =5
My Question Statement
if I insert the query(from original) :Update testing set column1 = 2 AND =4
the output
It's probably wrong to write 2 AND b = 1 though some databases evaluate it and others don't
If your intention is to set multiple columns in one go, you MUST use a comma
If you use this construct, perhaps some databases will process it according to bitwise-logical operation or they will treat any non zero value as true/false and any zero as false/true, and others will process it according to Boolean-logical operation
In the example strawberry kindly posted: https://www.db-fiddle.com/f/3KawkrD8QfjJu6YyfuzB7U/0
Set the db to MySQL and run it; it works out - MySQL is probably treating 2 as true, so the operation becomes:
SET c = true and true --when b = 1
SET c = true and false --when b=0
You can see your c column is set to 1 or 0 depending on the truth
SET c = (2 AND (b = 1))
^^^^^^^^^^^^^^^
This whole thing is turned into a value.
for C, it does NOT set column B at all
Now change the DB to Postgres and run it again
This time you get an error that AND expects Boolean operands, and the integer 2 is not acceptable
Hence in Postgres while this might be acceptable:
SET d = (c=1 AND b=2) --sets a true or false value for d
Your other form is not acceptable
--
Long story short, you probably intended to set multiple columns: use a comma.
We can see from this simple experiment...
https://www.db-fiddle.com/f/3KawkrD8QfjJu6YyfuzB7U/0
...that ...
UPDATE my_table SET c = 2 AND b = 1;
... is interpreted as...
UPDATE my_table SET c ........ = 1;
how can i do something like this:
I have two tables, A and B. A --> B is one to many.
B has a varchar field that we will call field1.
I have also a sequence seq1 that i want to use in this way:
Suppose we have A1 and A2, two records belonging to table A. A1 has (B1,B2,B3,B4) and A2 has (B5,B6).
I want to use the sequence for each group and start from the beginning each time i change group to update field1 in B. So i will have somthing like
B1.field1 = 1, B2.field1 = 2, B3.field1 = 3, B4.field1 = 4
now the sequence start back from 1 for A2:
B5.field1 = 1, B6.field2 = 2.
Is there some complex nested structure to do that or i need a function?
I was thinking about using a temporary table ,playing a bit on indexes and sub countings but i don't manage to find a wayout.
Thanks
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 have table pref having column value. This value has type text. I want copy the value field value of row with id 7 to the value field of row with id 1. Can you please help how to do this. I know MS SQL, but I am new to mySQL.
create table pref
(
id int,
value text
)
In MySQL you can't use a subselect from the same table you are updating, but you can use a join.
UPDATE pref AS target
LEFT JOIN pref AS source ON source.id = 7
SET target.value = source.value
WHERE target.id = 1;
UPDATE
pref
SET
value = (SELECT value WHERE id = 7)
WHERE
id = 1
In my case, I was trying to copy an encrypted value from one row into an empty field in another row in the same table. In this case, I needed to copy john's PIN into Jane's PIN.
#mohang has it right. Here is my adaptation :)
name pin
john sdhjduwhdowodw7d87e838g83g8g3of...
jane
//copy from field with value into empty field
UPDATE my_table AS target
LEFT JOIN my_table AS source ON source.pin != ""
SET target.pin = source.pin
WHERE target.pin = "";
// Hurray, it works!
name pin
john sdhjduwhdowodw7d87e838g83g8g3of...
jane sdhjduwhdowodw7d87e838g83g8g3of...