Get an extra comma ',' while using concat - mysql

I have wrote a stored proc in sqlyog. It is quite long and performing all the desired functionality except for the concat statement so I will only list that particular query.
UPDATE recipes_new
SET co_auths = CONCAT(co_auths,',',c1id)
WHERE id = name_in;
I basically want a separation in the two fields and this statement is placed in a cursor so it is iterative. co_auths is null at the moment so I get the result as ,1,2,3 where as I want it to be 1,2,3. Any guesses what can the most appropriate solution be?

By using an IF:
UPDATE recipes_new
SET co_auths = IF(co_auths IS NULL, c1id, CONCAT(co_auths, ',', c1id))
WHERE id = name_in;
If the value of co_auths is an empty string instead of NULL:
UPDATE recipes_new
SET co_auths = IF(LENGTH(co_auths), CONCAT(co_auths, ',', c1id), c1id)
WHERE id = name_in;

MySQL returning an empty field: CONCAT(nonEmpty1,empty2,nonEmpty3) = NULL
CONCAT_WS is what you are looking for
UPDATE recipes_new SET co_auths = CONCAT_WS(co_auths,',',c1id) WHERE id = name_in;

This should work using CASE to check if it's NULL:
CONCAT(
CASE
WHEN IFNULL(co_auths,'') = ''
THEN ''
ELSE CONCAT(co_auths, ',')
END, c1id)

Related

mysql update json attribute and another column in one query

I need to update a json value in a column as well as update another column in the same query.
Something like this:
UPDATE fixtures
SET jsonResults = '{}',
JSON_SET(jsonFixture, '$.time_status', '0')
WHERE intRefID = 88961323;
How can I accomplish this?
JSON_SET() returns a JSON document value, but an UPDATE statement needs a series of assignment expressions:
UPDATE fixtures
SET jsonResults = '{}',
jsonFixture = JSON_SET(jsonFixture, '$.time_status', '0')
WHERE intRefID = 88961323;
This replaces jsonFixture with the result of JSON_SET(), after setting a field within that document.
Compare with an UPDATE like this:
UPDATE mytable
SET i = i + 1
WHERE ...
It takes the value of i, adds 1, and then uses the result of that addition expression to replace i.

How to update a column only when last word of string matches a letter

I want to update columns(HostActvTyp and HostPrvTyp) when (HostCd) column last name is matching with last word of the string is "R"
example string for host column "NXVR','REACTIVE', so the last word is R so we have to update to Reactive when it's N' to 'NEW' example "XVDN"
enter image description here
seems you are looking for left or substr()
update tablename
set ActTyp = 'INDEPENDENT'
where ActvTyp = left(your_value,1 )
https://www.db-fiddle.com/f/bG4jZfnWksjc315eGTuG2k/1
UPDATE tablename
SET ActTyp = 'INDEPENDENT'
WHERE 'I' = RIGHT(ActvTyp, 1)
Or if there is a space separator
UPDATE tablename
SET ActTyp = 'INDEPENDENT'
WHERE ' I' = RIGHT(ActvTyp, 2)

MySQL update with sequential number yields NULL

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)))

Null value matching in mySql

I have three tables in a mysql database . Deseasetype(DTID,TypeName) , Symptom(SID, SymptomName, DTID) , Result(RID, SID1, SID2, SID3, result).1st two table, i think is clear enough.
In result table: there will be combination's of symtoms and any values of SymID1/ SymID2/ SymID3 can be null. here i send a picture of the table result.
I want to input some symptom and output will be the result from the 'Result' table.
For that i wrote this query:
$query = "select Result from result where (result .SID1= '$symptom1') AND (result.SID2= '$symptom2' ) AND (result.SID3 = '$symptom3')";
This work only when three symptom's have value. but if any of the symptom's are null, then no result found. May be the query should be more perfect.
**please avoid any syntax error in my writing.
That's because you are comparing NULL to an empty string, and they aren't equal. You could try this instead:
SELECT Result
FROM symptom
WHERE IFNULL(symptom.SID1, '') = '$symptom1'
AND IFNULL(symptom.SID2, '') = '$symptom2'
AND IFNULL(symptom.SID3, '') = '$symptom3'
Notes:
You need to correctly escape the values of $symptom1, $symptom2 and $symptom3.
This won't efficiently use indexes.
As mark pointed out, the query is eventually falling down to compare with null if you are not escaping the null.
Or you can slightly change your logic to show a empty symptom with value '0' and then using the coalesce function you can easily build your query.
Does this work?
$query = "select Result from result
where (result.SID1 = '$symptom1' OR result.SID1 IS NULL) AND
(result.SID2 = '$symptom2' OR result.SID2 IS NULL) AND
(result.SID3 = '$symptom3' OR result.SID3 IS NULL)";

SQL: How can I update a value on a column only if that value is null?

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