I'm new in MYSQL and I'm trying to do this:
UPDATE team
SET member1 = IF(member1=0, 001, SET member2 = IF(member2=0, 001, member2)) WHERE ID = ?;
This is not working, explain:
Each "member" has a different number (001, 002, 003...), I want to put the member "001" in the table "member1" but if this is ocupated (it has a value different of 0) set the member "001" in the table "member2" (a diferent table) but I think is not possible to put a SET inside of an IF. The id doesn't matters.
Thanks for attention ^^
No you can't join SET statements like this. It'll have to be worked around:
UPDATE `team`
SET
`member2` = CASE WHEN `member1` = 0 AND `member2` = 0 THEN '001' ELSE `member2` END,
`member1` = CASE WHEN `member1` = 0 THEN '001' ELSE `member1` END
WHERE
`id` = ?
/* optional filter: considering adding it if it does not bother the performance: */
AND (`member1` != 0 OR `member2` != 0)
;
Notes:
I used backticks around table and column names, I recommend you do so*
I used CASE/WHEN instead of IF, because IF is not ansi sql (it is specific to mysql)
You could also do two queries instead of one. That would allow you to use a filter in the where clause, so you don't have to update the column when it's not required.
*as pointed out by Bohemian, they are not ansi. I like to use them for the structural and visual help they provide.
Related
Please how do i update all record to 0 and set only the selected ID to 1
UPDATE address SET default_addr = 1
WHERE addr_id = 100 AND user = 'peter'
The above query will update the selected address to 1 which is good, but i want to set other address or the old selected default to 0 with one query
In MySQL, you can do:
UPDATE address
SET default_addr = (addr_id = 100 AND user = 'peter');
(This shorthand uses the fact that MySQL treats booleans as numbers in a numeric context, with "0" for false and "1" for true.)
If you want only one default address for the user named Peter, then use a where:
UPDATE address
SET default_addr = (addr_id = 100)
WHERE user = 'peter';
I suspect this is the logic that you really want.
use a conditional update using case statement
update address set default_address = case when addr_id = 100 and user = 'peter' then 1 else 0 end
here is a functional example
I built a sample schema. These are often helpful to provide in your future questions.
I want to update the values of a column with the name of a related entry appended with a number for each row. I've seen a couple of other questions which give roughly the same answer, but when I try the below, I get NULL added instead.
SET #i = 0;
UPDATE matrix_data md
SET col_id_4 = concat((SELECT title from titles t WHERE t.entry_id = md.entry_id), (#i:=#i+1));
If I replace (#i:=#i+1) with a static value, then the update works OK.
The col_id_4 column is set to text. Does the above only work with numeric column types? And if so, how do I achieve what I want to do?
How about you take the primary key of the titles table instead of using the iterator, thus filling in col_id_4 with a concatenation of titles.title and titles.entry_id, thus…
UPDATE matrix_data
INNER JOIN titles ON matrix_data.entry_id = titles.entry_id
SET matrix_data.col_id_4 = CONCAT(titles.title, "_", titles.entry_id)
Or maybe it’s a type issue; casting the iterator to char(50) as we concatenate with the string should work. Tested in a rudimentary database, not #craftcms or #eecms specifically.
SET #i := 0;
UPDATE matrix_data
INNER JOIN titles ON matrix_data.entry_id = titles.entry_id
SET matrix_data.col_id_4 = CONCAT(titles.title, "_", CAST(#i := #i + 1 as CHAR(50)))
I want to write a query that will verify 8 parameters of a row are the same as the parameters passed to the procedure. If any one of them is different then modify a status, otherwise do nothing.
Is there a way I can check all those parameters in a single IF case? For example:
IF (v_duty <> duty) OR (v_current <> current) OR (v_frequency <> frequency) THEN
* UPDATE ......;
END IF
Or do I have to use an ELSE IF for each comparison I want to make?
The above doesn't work, with or without brackets between each test.
You could probably manage this with a simple UPDATE and WHERE condition:
UPDATE table_name
SET status_column = 'new_status'
WHERE identifying_column = :identifier
AND (
v_duty != :v_duty
OR v_current != :current
OR v_frequency != :frequency
)
How do I correctly replace the first character with 'M'? Suppose you have a PATIENT_ID_NONNUM = 'M001', and we want 1001 as a result.
UPDATE [HIMC_I2B2_LZ-PROD].[dbo].[I2B2_SRC_BIOMETRICS]
SET PATIENT_ID = CONVERT(NUMERIC(22,0),'1' + CONVERT(NVARCHAR(50),PATIENT_ID))
WHERE SUBSTRING(PATIENT_ID_NONNUM, 1, 1) = 'M'
EDIT:
UPDATE [HIMC_I2B2_LZ-PROD].[dbo].[I2B2_SRC_MEDICATION]
SET PATIENT_ID = CONVERT(NUMERIC(22,0),CONVERT(NVARCHAR(50),'1') + CONVERT(NVARCHAR(50),SUBSTRING(PATIENT_ID_NONNUM, 2, LEN(PATIENT_ID_NONNUM))))
WHERE SUBSTRING(PATIENT_ID_NONNUM, 1, 1) = 'M'
I find STUFF() (an often overlooked function) and LEFT() are a little more readable, but others may disagree:
UPDATE [HIMC_I2B2_LZ-PROD].[dbo].[I2B2_SRC_BIOMETRICS]
SET PATIENT_ID = CAST(STUFF(PATIENT_ID_NONNUM, 1, 1, '1') AS NUMERIC(22,0))
WHERE LEFT(PATIENT_ID_NONNUM, 1) = 'M'
I would suggest something like this:
UPDATE [HIMC_I2B2_LZ-PROD].[dbo].[I2B2_SRC_BIOMETRICS]
SET PATIENT_ID = CAST(('1' + SUBSTRING(PATIENT_ID_NONNUM, 2, LEN(PATIENT_ID_NONNUM) - 1)) AS NUMERIC(22,0))
WHERE SUBSTRING(PATIENT_ID_NONNUM, 1, 1) = 'M'
That's going to find all records where the first character is M, and replace the first character with a 1. I haven't tested this, but I believe it should work properly.
I would also suggest not running this type of operation on a production database as a test, which I would assume is what the -PROD stands for in your catalog name.
EDIT: Since it seems important that this query comes out with the PATIENT_ID as a NUMERIC(22,0), I've added the necessary CAST.
I have an SQL question which may be basic to some but is confusing me.
Here is an example of column names for a table 'Person':
PersonalID, FirstName, LastName, Car, HairColour, FavDrink, FavFood
Let's say that I input the row:
121312, Rayna, Pieterson, BMW123d, Brown, NULL, NULL
Now I want to update the values for this person, but only if the new value is not null, Update:
121312, Rayna, Pieterson, NULL, Blonde, Fanta, NULL
The new row needs to be:
121312, Rayna, Pieterson, BMW123d, Blonde, Fanta, NULL
So I was thinking something along the lines of:
Update Person(PersonalID, FirstName, LastName, Car, HairColour,
FavDrink, FavFood) set Car = #Car (where #Car is not null), HairColour
= #HairColour (where #HairColour...)... etc.
My only concern is that I can't group all the conditions at the end of the query because it will require all the values to have the same condition. Can't i do something like Update HairColour if #HairColour is not Null
Id use coalesce for this:
http://msdn.microsoft.com/en-us/library/ms190349.aspx
update Person
set Car = coalesce(#Car, Car), HairColour = coalesce(#HairColour, HairColour)
The following should work:
UPDATE Person
SET Car = ISNULL(#Car, Car),
HairColour = ISNULL(#HairColour, HairColour),
...
It uses the SQL Server ISNULL function, which returns
the first value if it is non-null,
or, otherwise, the second value (which, in this case, is the current value of the row).
You can use the isnull function:
update Person
set
Car = isnull(#Car, Car),
HairColour = isnull(#HairColour, HairColour),
FavDrink = isnull(#FavDrink, FavDrink),
FavFood = isnull(#FavFood, FavFood)
where PersonalID = #PersonalID
Set the column equal to itself with an isnull round it setting it to your parameter.
UPDATE
YourTable
SET
YourColumn = ISNULL(YourColumn, #yourParameter)
WHERE
ID = #id