MySQL set value using text and another field - mysql

Okay, not quite sure how to explain it but I'll try my best...
I want to run a MySQL query to set names to 'Unmanaged Team [id]'
My thoughts were initially to use the following (but it doesn't work as hoped).
mysql_query("UPDATE `table` SET `field`='Unmanaged Team'+`id`");
I've tried some variations of that, but I can't quite seems to figure it out, and I don't want to do them one by one, as that would just use far too many queries for something I'm sure can be done in 1.
Many thanks for taking some time to help me out :)

You can use concat:
update `table` set `field` = concat('Unmanaged Team', id);
concat_ws (concat with separator) is also useful if you want whitespace (or some other separator).

Use
SET field = CONCAT('something ',somefield)

Related

How do I remove an email domain value and add a new one in a column - mysql

So I have a bunch of users in a column that get refreshed as:
Bill#test.comXYZ
Tom#test.comXYZ
John#test.comXYZ
We refresh the database each week and I need to update these appropriate emails to:
Bill#domain.com
Tom#domain.com
John#domain.com
I figured I can use concat to do the latter, but I am stuck on the former issue. Is there a way to split the values (like split Bill#test.comXYZ into Bill - #test.comXYZ and then remove the #TEXT values?).
Anyways, any help will be much appreciated.
You can use the mySQL replace function, i.e.
UPDATE mytable
set myfield = replace (myfield, '#test.comXYZ', 'domain.com')
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace

Whats wrong with this MySQL Update Query?

1st I'll give you the query, and then I'll tell you what I am trying to achieve, as I could be soo wrong or soo close.
mysql_query("UPDATE link_building SET
ID=$ID,Site=$Site,Date=$Date,Target_Site=$Target_Site,
Target_Contact_Email=$Target_Contact_Email,
Target_Contact_Name=$Target_Contact_Name,
Link_Type=$Link_Type,Link_Acquired=$Link_Acquired,
Notes=$Notes,Link_URL=$Link_URL WHERE ID=" . $ID);
What am I trying to achieve?
I want to update the fields
("ID","Site","Date","Target_Site","Target_Contact_Email","Target_Contact_Name",
"Link_Type","Link_Acquired","Notes","Link_URL")
in the table link_building with the values stored in the variables
("$ID","$Site","$Date","$Target_Site","$Target_Contact_Email","$Target_Contact_Name",
"$Link_Type","$Link_Acquired","$Notes","$Link_URL")
But I only want to update the record whos Id is equal to $ID.
UPDATE: I DO NOT SEE ANY ERROR. ITS REDIRECTS TO link_building.php and displays success message but doesn't change the data in the MySQL table.
Try escaping the data and removing the update of the ID since its already in your conditions:
mysql_query("UPDATE link_building SET Site='".mysql_real_escape_string($Site)."',Date='".mysql_real_escape_string($Date)."',Target_Site='".mysql_real_escape_string($Target_Site)."', Target_Contact_Email='".mysql_real_escape_string($Target_Contact_Email)."', Target_Contact_Name='".mysql_real_escape_string($Target_Contact_Name)."', Link_Type='".mysql_real_escape_string($Link_Type)."',Link_Acquired='".mysql_real_escape_string($Link_Acquired)."', Notes='".mysql_real_escape_string($Notes)."',Link_URL='".mysql_real_escape_string($Link_URL)."' WHERE ID=" . intval($ID));
For one, you're forgetting that you still need to quote your strings;
mysql_query("UPDATE link_building SET Site='$Site', Date='$Date',".
"Target_Site='$Target_Site', Target_Contact_Email='$Target_Contact_Email',".
"Target_Contact_Name='$Target_Contact_Name', Link_Type='$Link_Type',".
"Link_Acquired='$Link_Acquired', Notes='$Notes', Link_URL='$Link_URL' ".
"WHERE ID=$ID");
Note the added 's around all strings.
Bonus remark; you should really be using mysql_real_escape_string() on your strings before passing them on to the database.
if your columns are named like Target Site (with a space in it), you should adress it like that in your query (wich will force you to add backticks to it). also, you'll have to add quotes to colums that store anything else that strings. your query should look like:
UPDATE
link_building
SET
ID = $ID,
Site = '$Site', // single quotes for values
Date = '$Date', // ...
´Target Site´ = '$Target_Site' // and ´ for fields
[...]
this should solve why the query doesn't work (in addition: not how a bit or formatting makes it much more readable).
you havn't given information about that, but please note that you should always sanitize your variables before using it (your code doesn't look like you do) to avoid sql-injections. you can do this using mysql_real_escape_string or, even better, start using prepared statements.

Update MySQL Column with WHERE condition

I need to change the information in a column to Degraded, where it is Feature Broken or Degradated. Is this the correct line?
UPDATE tablename
SET column = 'Degraded'
WHERE column = 'Degradated' OR column= 'Feature Broken'
Thanks
You may want to use an IN operator, to avoid the OR:
UPDATE tablename SET columnname = 'Degraded' WHERE columnname IN ('Degradated', 'Feature Broken');
Also, I would suggest running a SELECT first so you are somewhat aware of how many rows will be changed with your command.
Yes, it is.
You could make it more concise (but it is exactly the same otherwise)
...
WHERE column IN ('Degradated','Feature Broken')

Escaping % sign in subquery

I have a query in mySQL that's meant to return search terms that are used on our site. Yes, this is for a tag cloud, and yes, I know it's a mullet :)
We've got an admin page where administrators can view search terms and choose to exclude them from showing up in the cloud. These words go into the "badWords" table. We've gotten some terms like "foo%2525252525252520bar", and we're having trouble getting those excluded.
In pseudocode, the query to get the search terms for the cloud is:
SELECT * FROM `searchTerms` WHERE `word` NOT IN ( SELECT `word` FROM `badWords` )
This works fine, unless one of the terms returned from the subquery has a % in it. Is there a way to escape the entire subquery? I've tried doing a
replace( SELECT `word` FROM `badWords`, '%', '\%' )
... but that's apparently not syntactically correct.
I can do two queries if need be, but wondered if there's a way to get it done as is.
Thanks!
==============================
UPDATE: closing this for now, as I think the error lies elsewhere. Will report back once I know for sure, but don't want folks wasting time answering the question here if it's not the correct question...
Upvoted both of the replies received so far. Thanks, guys.
==============================
UPDATE 2: sigh Nevermind... can't close it :\
==============================
FINAL UPDATE: Well, looks like escaping the value isn't the problem. The admin page passes the value in the URL before it's added to the badWords table. In passing the value via the URL, it changes. So what's added to badWords is actually "foo%25252525252520bar" (there's one less "25" sequence). If I manually update the value in badWords and add back the missing "25" it works as expected. So no need to replace or escape anything. I just need to fix those URL values properly.
==============================
I don't think the % is your problem here. I think that you're trying to use REPLACE() on the subquery itself (SELECT ...), and not on a column value (word). Try this instead:
SELECT * FROM `searchTerms`
WHERE `word` NOT IN (
SELECT REPLACE(`word`, '%', '\%') AS word FROM `badWords`
);
Good luck!
I'm not very good with MySQL syntax, but SQL Server let's you do it this way:
SELECT * FROM `searchTerms` WHERE `word` NOT IN ( SELECT REPLACE(`word`, '%', '\%') FROM `badWords` )
NOTE: Basically all I did was move your REPLACE over some =) Hope this helps.

MYSQL Concat not working

UPDATE profile SET
favourties=CONCAT(favourties,"123")
WHERE id=1
i want to append 123 in favourties but if default value of favourties is set to NULL then this query will not work. What will be query if favourties is set to NULL and then append 123 with it
UPDATE profile SET favourties=CONCAT(IFNULL(favourties, ''),"123") WHERE id=1
Wrap the field around with the COALESCE function:
UPDATE profile
SET favourties = CONCAT(COALESCE(favourties, ''),"123")
WHERE id=1
You probably can't concatenate something to NULL. Maybe you can use coalesce?
UPDATE profile SET favourties=CONCAT(COALESCE(favourites,""),"123") WHERE id=1
see: http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce
You can use ifnull like #zerkms says, but that is not in the SQL standard. It is a tiny tad faster though. Read up on it at this link: http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/performance-isnull-vs-coalesce.aspx
You can also use CONCAT_WS() (docs) which deals with NULLs as you would expect: converts them to an empty string:
UPDATE profile SET favourties = CONCAT_WS('', favourties, "123") WHERE id = 1;
I personally actually only use CONCAT_WS() now because having to worry about NULLs annoys me. I rarely use NULLs so I don't have to worry about it, but just incase. It's just an annoying thing to figure out why you you're ending up with an empty string when it just doesn't seem to make sense.
In PHP, I use:
SET `trans`=concat('$var', trans)
to add to a string already in the trans column.
It wouldn't work on column named group without using back ticked group inside the brackets as well, whereas with trans, back ticks were not needed.