If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Update query to append text from one field to another

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-26-11, 00:09
Trumpet Trumpet is offline
Registered User
 
Join Date: Apr 2003
Location: China
Posts: 54
Question Update query to append text from one field to another

Hi,

I have two columns/fields:
1. fulltext [mediumtext]
2. images [text]

I need to append the contents of the images column to the fulltext column and add some other standard text as well. At this stage I have:

Code:
UPDATE jos_content 
SET `fulltext` = '<img src="/images/stories/' + images + '"/>' + `fulltext`
It seems to run, but it basically resulted in deleting all of my fulltext column entries.

I was going to add this to focus just on the images entries with content:
Code:
WHERE images NOT NULL
...but this won't solve the deletion problem.

I guess the field types could be a problem, and the double parenthisis "" could be the other... any help is very much appreciated.
Reply With Quote
  #2 (permalink)  
Old 02-26-11, 02:05
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
mysql does not use the plus sign for concatenation

it seems you have some microsoft sql server bad habits to unlearn
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-26-11, 03:05
Trumpet Trumpet is offline
Registered User
 
Join Date: Apr 2003
Location: China
Posts: 54
Ahhh, thanks for this.

I take it I should use & ?
Reply With Quote
  #4 (permalink)  
Old 02-26-11, 04:44
Trumpet Trumpet is offline
Registered User
 
Join Date: Apr 2003
Location: China
Posts: 54
I have changed the + to & but still get an error. I have also tried removing all the " , / , and > from the text I wanted to append but still with errors....

Any more specific help possible? thanks in advance.
Reply With Quote
  #5 (permalink)  
Old 02-26-11, 05:01
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_concat
Reply With Quote
  #6 (permalink)  
Old 02-26-11, 06:58
Trumpet Trumpet is offline
Registered User
 
Join Date: Apr 2003
Location: China
Posts: 54
Great... getting there, but unfortunately the follows still throws an error in the SQL:

Code:
UPDATE jos_content 
SET `fulltext` = CONCAT('<img src="/images/stories/',CAST(images AS MEDIUMTEXT),'"/>',`fulltext`) 
WHERE images NOT NULL
Reply With Quote
  #7 (permalink)  
Old 02-26-11, 07:12
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Quote:
Originally Posted by Trumpet View Post
Great... getting there, but unfortunately the follows still throws an error in the SQL
my copy of Microsoft® CrystalBall© is down at the moment, and i can't see the error message from here, any chance you can post it?

i mean, i can guess, but i'm trying to make a point to you about how to ask for help in forums...
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 02-26-11, 07:19
Trumpet Trumpet is offline
Registered User
 
Join Date: Apr 2003
Location: China
Posts: 54
Sorry. I am using Navicat for the first time and until you asked I didn't realise the error said more than just "error: you have an error in your SQL". However, clicking on this I realised the full error is:

error : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'MEDIUMTEXT),'"/>',`fulltext`)
WHERE images, `fulltext` NOT NULL' at line 2

Sorry for the omission. Definately appreciate your help.
Reply With Quote
  #9 (permalink)  
Old 02-26-11, 08:29
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Quote:
Originally Posted by Trumpet View Post
Sorry for the omission. Definately appreciate your help.
no problem, it shows you are willing to learn

the error message says the query died on MEDIUMTEXT inside the CAST function

go to da manual and look up the CAST function, and check out what the allowable options are

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 02-26-11, 09:59
Trumpet Trumpet is offline
Registered User
 
Join Date: Apr 2003
Location: China
Posts: 54
Fantastic! Thanks for your patience r937, I have figured the mysteries of the manual!

I have got this to run to run:

Code:
UPDATE jos_content 
SET `fulltext` = CONCAT('<img src="/images/stories/',CAST(images AS CHAR),'"/>',`fulltext`) 
WHERE images IS NOT NULL
It updated the 500+ rows with images for me. Fantastic.

I wasn't able to get the "WHERE images AND `fulltext` IS NOT NULL" to work however as zero rows were updated for some reason. Have read several pages in the manual on the use of NULL and IS NOT NULL but can't find the issue with the two.
Reply With Quote
  #11 (permalink)  
Old 02-26-11, 10:11
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
The syntax is
Code:
WHERE images IS NULL 
  AND fulltext IS NULL
Reply With Quote
  #12 (permalink)  
Old 02-26-11, 11:23
Trumpet Trumpet is offline
Registered User
 
Join Date: Apr 2003
Location: China
Posts: 54
Thanks, I got it sorted. I had tried this syntax but was still getting a strange outcome... turns out (I guess as fulltext was a text string or something) I needed to say:

Code:
WERE images IS NULL AND fulltext <> ""
Thanks for everyone's help!
Reply With Quote
  #13 (permalink)  
Old 02-26-11, 12:34
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Quote:
Originally Posted by Trumpet View Post
(I guess as fulltext was a text string or something)
it's a keyword -- FULLTEXT is a type of index

Code:
`fulltext` IS NOT NULL
or
Code:
`fulltext` > ''
you need to escape it with backticks (or preferably rename it)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On