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 > this query might be a challenge. Can you do it?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-01-11, 15:01
Leafgreen Leafgreen is offline
Registered User
 
Join Date: Apr 2008
Posts: 36
Exclamation this query might be a challenge. Can you do it?

I have a table with cells* that contain the following text
Quote:
vvvvv ssssss yyyyy tttttt
where vvvvv and yyyyy always have variable values, but not variable in position in relation to sssss and tttttt. The sssss and tttttt values are always the same/static.

I want to move
Quote:
ssssss yyyyy tttttt
(not including vvvvv variable text) to append to the end of another column ("columnB") with the same rows and table. Can you reply with the query, please?

*I use the term "cell" to mean the intersection of one row and one column in a table. Hope that's correct.

Thank you,
Leafgreen

Last edited by Leafgreen; 07-01-11 at 15:35. Reason: re-written to simplify in response to @mike's post
Reply With Quote
  #2 (permalink)  
Old 07-01-11, 15:13
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Understanding your query is certainly a challenge - I read your question twice and still can't work out what you want. You'll get a better standard of response if you just post a few examples of what you need.
__________________
Mike
Reply With Quote
  #3 (permalink)  
Old 07-02-11, 05:13
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
You've changed your original query but it really isn't any clearer to me. Does the string you want moving always start in exactly the same position or is it after a certain pattern is found in the original string? If you post a few examples then this will be quite clear and I can help you. If you can't then I suggest you go to the manual page on MySQL string functions and look up instr and substr.
__________________
Mike
Reply With Quote
  #4 (permalink)  
Old 07-02-11, 16:25
Leafgreen Leafgreen is offline
Registered User
 
Join Date: Apr 2008
Posts: 36
Thanks for your persistence with me, Mike.
Quote:
Originally Posted by mike_bike_kite View Post
Does the string you want moving always start in exactly the same position or is it after a certain pattern is found in the original string?
Yes, ssssss always follows vvvvv. But, vvvvv always has variable values.

Examples:
Cell value of ColumnA, row1: vvvvv ssssss yyyyy tttttt
Cell value of ColumnA, row2: 1234 xyz ssssss Dbforums is awesome.tttttt

Cell value of ColumnB, row1: uuuuuuu
Cell value of ColumnB, row2: append tothe endofthis

Run query. Result:
Cell value of ColumnA, row1: vvvvv
Cell value of ColumnA, row2: 1234 xyz

Cell value of ColumnB, row1: uuuuuuu ssssss yyyyy tttttt
Cell value of ColumnB, row2: append tothe endofthis ssssss Dbforums is awesome.tttttt

So, note that ssssss and tttttt remain unchanged before and after the query in both rows. They and the second set of variable text no longer remain in ColumnA, row1 and row2. And the designated values are moved to ColumnB, row1 and row2.
Reply With Quote
  #5 (permalink)  
Old 07-02-11, 17:19
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
The important bit is the 2 updates:
Code:
create table tmp(
        colA varchar(255),
        colB varchar(255)
);

insert tmp values ( "vvvvv ssssss yyyyy tttttt","uuuuuuu" );
insert tmp values ( "1234 xyz ssssss Dbforums is awesome.tttttt","uuuuuuu" );

update tmp
set colB = concat( colB, substr( colA, instr(colA," ") ) );

update tmp
set colA = substr( colA, 1, instr(colA," ") );

select * from tmp;

drop table tmp;
__________________
Mike
Reply With Quote
  #6 (permalink)  
Old 07-02-11, 17:27
Leafgreen Leafgreen is offline
Registered User
 
Join Date: Apr 2008
Posts: 36
I should have explained that the table contains hundreds of rows, and the variable text is variable in each row.
Reply With Quote
  #7 (permalink)  
Old 07-02-11, 17:38
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
My solution works for all the examples you supplied.
It should work whether there is 1 row or millions.

Please supply examples of where it didn't work or would you like to restart the game where you half specify the problem and we all have to guess what the issue is?
__________________
Mike
Reply With Quote
  #8 (permalink)  
Old 07-02-11, 17:49
Leafgreen Leafgreen is offline
Registered User
 
Join Date: Apr 2008
Posts: 36
I'm not playing a game here. Please respect that this a matter of us understanding each other in good faith...

I'm trying to explain that the variable values are different in every row. Let's put it this way: In the place of vvvvv and yyyyy are random characters. Will your query work if those values are random in each row?
Reply With Quote
  #9 (permalink)  
Old 07-02-11, 18:12
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Will your query work if those values are random in each row?
Yes of course - assuming the random characters don't contain spaces. I suggest you copy the tables you're doing the work on. Then run the supplied updates on the new tables and see if everything is fine. Then run the updates on the main table.
__________________
Mike
Reply With Quote
  #10 (permalink)  
Old 07-02-11, 18:15
Leafgreen Leafgreen is offline
Registered User
 
Join Date: Apr 2008
Posts: 36
Yes, the random characters do contain spaces, as in the example 1234 xyz and Dbforums is awesome.
Reply With Quote
  #11 (permalink)  
Old 07-03-11, 04:22
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
So how do you tell where the bit you want to keep ends?
__________________
Mike
Reply With Quote
  #12 (permalink)  
Old 07-03-11, 05:04
Leafgreen Leafgreen is offline
Registered User
 
Join Date: Apr 2008
Posts: 36
Quote:
Originally Posted by mike_bike_kite View Post
the bit you want to keep
To which parts are you referring in the example?
Reply With Quote
  #13 (permalink)  
Old 07-03-11, 05:46
Ikviens Ikviens is offline
Registered User
 
Join Date: Mar 2006
Posts: 55
Assuming two things, I find the string operation easy. First, ssssss occurs only once in the field. Second, tttttt marks the end of the field. In other words, there is no more than one ssssss or tttttt. When you find tttttt in the field, that's the end of the field.

To begin with, you should find out what part you want to keep intact from the older field:
Code:
SUBSTR(colA, LOCATE('ssssss', colA))
Then, prefix whatever you like:
Code:
SELECT colA, CONCAT('uuuuuuu ', SUBSTR(colA, LOCATE('ssssss', colA)))from tmp;
The first argument of CONCAT can be a literal or a field name.
Reply With Quote
  #14 (permalink)  
Old 07-05-11, 18:19
Leafgreen Leafgreen is offline
Registered User
 
Join Date: Apr 2008
Posts: 36
No, ssssss occurs only in some rows in ColumnA. Likewise, tttttt occurs and marks the end of only some rows in ColumnA. I only want the query to be activated in those rows where ssssss and tttttt occur.
Reply With Quote
  #15 (permalink)  
Old 07-05-11, 21:04
Ikviens Ikviens is offline
Registered User
 
Join Date: Mar 2006
Posts: 55
Use a condition like below in your WHERE clause:
LOCATE('ssssss', colA)*LOCATE('tttttt', colA) > 0
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