Results 1 to 12 of 12

 

Thread: MYSQL - How to search and delete?

  1. #1
    Frostie's Avatar
    Moderator

    Status
    Offline
    Join Date
    Aug 2003
    Location
    Wirral
    Posts
    3,217
    Thanks
    61
    Thanked 60 Times in 27 Posts


    I'm wanting to run an SQL command from phpmyadmin that will search a FIELD in a TABLE and look for "xxxxx". Once it has found it, I want it to delete xxxxx and everything after it.

    I can find all the posts by running a search such as:
    Code:
    SELECT * FROM `TABLE` WHERE `FIELD` LIKE '%xxxxx%'
    I know how to do a simple search and replace for EXACT matches such as:
    Code:
    UPDATE TABLE SET FIELD = replace(FIELD,"xxxxx","yyyyy");
    Using the above, I could find xxxxx and replace it with "" - however I want to delete xxxxx and everything after it, in the field. The content that follows xxxxx constantly changes so it has to be open ended.

    Can anyone offer any advice?

  2. #2
    philhancox's Avatar
    Registered User

    Status
    Offline
    Join Date
    Oct 2007
    Posts
    513
    Thanks
    4
    Thanked 18 Times in 16 Posts
    If I understand you correctly, I would do

    Code:
    UPDATE TABLE SET FIELD='' WHERE FIELD LIKE 'XXXX%'
    PhilHancox.co.uk | Affiliate with sites including discount codes, etc etc | I'm on Twitter

  3. The Following User Says Thank You to philhancox For This Useful Post:

    Frostie (14-08-09)

  4. #3
    Frostie's Avatar
    Moderator

    Status
    Offline
    Join Date
    Aug 2003
    Location
    Wirral
    Posts
    3,217
    Thanks
    61
    Thanked 60 Times in 27 Posts
    Surely it's not that easy!

    I've just tried it and sadly it didn't work. It comes back with the MYSQL returned an empty result set (i.e. zero rows) error. I think this is due to the % only being on one side. If I change it %xxxxx% then it does obtain results, however I don't want any text BEFORE xxxxx to be deleted, only after.

  5. #4
    Registered User

    Status
    Offline
    Join Date
    May 2008
    Posts
    152
    Thanks
    2
    Thanked 5 Times in 5 Posts
    surely that would set the whole field to blank, whereas I was under the impression you wanted to keep what was in front of the 'xxxxx' string?

  6. #5
    philhancox's Avatar
    Registered User

    Status
    Offline
    Join Date
    Oct 2007
    Posts
    513
    Thanks
    4
    Thanked 18 Times in 16 Posts
    Quote Originally Posted by mrbishi View Post
    surely that would set the whole field to blank, whereas I was under the impression you wanted to keep what was in front of the 'xxxxx' string?
    Yes it will; if the XXXX string is not at the beginning then we have a different story and I'll write something new. Frostie?
    PhilHancox.co.uk | Affiliate with sites including discount codes, etc etc | I'm on Twitter

  7. #6
    Frostie's Avatar
    Moderator

    Status
    Offline
    Join Date
    Aug 2003
    Location
    Wirral
    Posts
    3,217
    Thanks
    61
    Thanked 60 Times in 27 Posts
    Yup thats what I am trying to do.... so

    RECORD 1
    has this text and that text xxxxx then mumbo jumbo

    RECORD 2
    may have something like this and that before we see xxxxx that should be deleted

    So the stuff in bold gets deleted (i.e. anything after xxxxx). Don't you just love my childish way of explaining stuff :blush

  8. #7
    Registered User

    Status
    Offline
    Join Date
    May 2008
    Posts
    152
    Thanks
    2
    Thanked 5 Times in 5 Posts
    I've been having a look at the replace function and it seems that you can replace substrings, but im not sure if you can use that with the like function yet, am investigating. - can do it using php but not pure sql just yet!

  9. #8
    philhancox's Avatar
    Registered User

    Status
    Offline
    Join Date
    Oct 2007
    Posts
    513
    Thanks
    4
    Thanked 18 Times in 16 Posts
    Ditto,
    I'd do something like fetching the array, do a str_replace on the fields then update the database with the results. It would have to be a PHP script though
    PhilHancox.co.uk | Affiliate with sites including discount codes, etc etc | I'm on Twitter

  10. #9
    Working Hard

    Status
    Offline
    Join Date
    Dec 2006
    Location
    Poole
    Posts
    117
    Thanks
    11
    Thanked 8 Times in 8 Posts
    I'm not too sure on MySQL Syntax, but converting what I would do in SqlServer to MySQL via the internet I would say you want to do:

    Code:
    UPDATE TABLE SET FIELD = LEFT(FIELD, LOCATE('xxxxx',FIELD)) WHERE FIELD LIKE '%XXXXX%'

  11. The Following User Says Thank You to KamikzMX For This Useful Post:

    Frostie (14-08-09)

  12. #10
    Registered User

    Status
    Offline
    Join Date
    May 2008
    Posts
    152
    Thanks
    2
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by philhancox View Post
    Ditto,
    I'd do something like fetching the array, do a str_replace on the fields then update the database with the results. It would have to be a PHP script though
    yeah i'd explode on the 'xxxxx' string and replace with the the first element of the array. But as you said would need to be a php script as sql's string manipulation strings are quite basic.

  13. The Following User Says Thank You to mrbishi For This Useful Post:

    Frostie (14-08-09)

  14. #11
    Registered User

    Status
    Offline
    Join Date
    May 2008
    Posts
    152
    Thanks
    2
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by KamikzMX View Post
    I'm not too sure on MySQL Syntax, but converting what I would do in SqlServer to MySQL via the internet I would say you want to do:

    Code:
    UPDATE TABLE SET FIELD = LEFT(FIELD, LOCATE('xxxxx',FIELD)) WHERE FIELD LIKE '%XXXXX%'
    awesome. top solution, i'd been looking at the left function but couldn't work out how to set the position parameter

  15. #12
    Frostie's Avatar
    Moderator

    Status
    Offline
    Join Date
    Aug 2003
    Location
    Wirral
    Posts
    3,217
    Thanks
    61
    Thanked 60 Times in 27 Posts
    Thanks all :tup

    Got it working using KamikzMX code. Happy days



Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. PHP Search & Delete
    By Frostie in forum Programming
    Replies: 8
    Last Post: 06-10-06, 01:20 PM
  2. Mysql search query
    By Melter in forum Programming
    Replies: 2
    Last Post: 19-11-04, 04:35 PM
  3. php and mysql search
    By andy_jacko in forum Programming
    Replies: 6
    Last Post: 13-09-04, 12:12 PM
  4. How to delete with php and mysql
    By lakrasia in forum Programming
    Replies: 1
    Last Post: 18-06-04, 03:45 PM
  5. MySql Table Search
    By holmes in forum Programming
    Replies: 2
    Last Post: 17-04-04, 06:09 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
To Top

Content Relevant URLs by vBSEO 3.5.0 RC2