DotDragnet
May 21, 2012, 05:21:45 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: follow us on twitter @dotdragnet
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: SQL Query  (Read 2067 times)
Charisma Bypass
Hero Member
*****
Posts: 556



View Profile Awards
« on: November 03, 2007, 03:33:02 PM »

If I had a bunch of records in which 1 row contained a certain words (let's say it's "plonker" for arguments sake), what SQL would I run to entirely remove any row which contains the word plonker?

Eg: 

ID: 1
Name: Burt
Content:  is a plonker

How would I make "Content" blank?  But for all the records in the DB that contained the word.  And if Content didn't contain "plonker" it would just stay as is, unchanged.

Thanks in advance for any help.
Logged
Charisma Bypass
Hero Member
*****
Posts: 556



View Profile Awards
« Reply #1 on: November 03, 2007, 03:36:27 PM »

Hmmmn.

Code:
DELETE FROM Table WHERE Content LIKE '%plonker%'

That should do it?  Or maybe

Code:
UPDATE Table SET Content = '' WHERE Content LIKE '%plonker%'

Would be better?
« Last Edit: November 03, 2007, 03:38:06 PM by Charisma Bypass » Logged
sarahA
DDN Contribs
Hero Member
*****
Posts: 2174



View Profile WWW Awards
« Reply #2 on: November 03, 2007, 03:38:06 PM »

this will empty the content field

Code:
UPDATE tablename SET content = '' WHERE content LIKE '%plonker%'

this will remove the entire record

Code:
DELETE FROM tablename WHERE content LIKE '%plonker%'

This will find any version of the word plonker however, be it a whole word or part of a word (eg. splonker). If you want just words then you'd need to do something like

Code:
content LIKE '% plonker %' OR content LIKE '% plonker,%' OR content LIKE '% plonker.%'

and so on, covering any type of symbol that could be after the word.

hth
Logged

JasonD
Global Moderator
Hero Member
*****
Posts: 546



View Profile Awards
« Reply #3 on: November 03, 2007, 04:28:19 PM »

Quote
and so on, covering any type of symbol that could be after the word.

WHERE content REGEXP '(^|[^[:alpha:]])plonker($|[^[:alpha:]])'
Logged
sarahA
DDN Contribs
Hero Member
*****
Posts: 2174



View Profile WWW Awards
« Reply #4 on: November 03, 2007, 05:40:21 PM »

Or that too... wink
Logged

Charisma Bypass
Hero Member
*****
Posts: 556



View Profile Awards
« Reply #5 on: November 03, 2007, 08:45:03 PM »

Thanks guys, you've really helped me out smile
Logged
Charisma Bypass
Hero Member
*****
Posts: 556



View Profile Awards
« Reply #6 on: November 04, 2007, 10:17:37 AM »

Say I have a row (fulltext) which contains 10 paragraphs of text.  How would I strip out the first 2 or 3 paragraphs of text from it and put the stripped out stuff into a different row?

Basically, create an `excerpt` taken from some `content`...

Thanks in advance, you lovely people!
Logged
sarahA
DDN Contribs
Hero Member
*****
Posts: 2174



View Profile WWW Awards
« Reply #7 on: November 04, 2007, 10:32:47 AM »

I think (although you'd need to try this or wait for Jason or Jee to come along and give a better version..!) you could do

Code:
UPDATE tablename SET excerpt = LEFT(content, 200)

That would just copy it over. If you want to then remove that from the content as well then this may work

Code:
UPDATE tablename SET excerpt = LEFT(content, 200), content = RIGHT(content, LENGTH(content)-200)

However I'm not 100% certain the second bit would work in one query, you may need to do the first one and then the second.
Logged

JasonD
Global Moderator
Hero Member
*****
Posts: 546



View Profile Awards
« Reply #8 on: November 04, 2007, 01:50:36 PM »

Code:
UPDATE table SET excerpt = CONCAT(SUBSTRING_INDEX(content, '</p>', 3), '</p>')

Where 3 is the number of paragraphs and '</p>' is how they are marked, you have to concat it back in as it will cut off before it. if it is plain text then you probably want '\r\n\r\n' instead of '</p>'.

If you want to remove the excerpt from the rest of the content you will need a second query:
Code:
UPDATE table SET content = SUBSTRING(content, LENGTH(excerpt) + 1)
« Last Edit: November 04, 2007, 01:52:28 PM by JasonD » Logged
Charisma Bypass
Hero Member
*****
Posts: 556



View Profile Awards
« Reply #9 on: November 04, 2007, 03:46:51 PM »

Thanks Guys.  Very much appreciated.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2008, Simple Machines Valid XHTML 1.0! Valid CSS!