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 > Reverse content of table with condition

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-25-11, 05:29
madkitty madkitty is offline
Registered User
 
Join Date: Apr 2011
Posts: 18
Question Reverse content of table with condition

Hi there,

I have a table that looks like that:
(+or-) (smaller number) (bigger number)
col1 col2 col3
+ 100 115
- 155 116
+ 225 250
- 300 275
+ 325 350


Everytime that we have '-' in col1 i need to reverse the number col2<-->col3

I created a temporary table called INVERSE(with 2 columns a and b) to store numbers temporarily in it and then paste them in the right column. But my query doesn't work... How can I simply reverse the content

Code:
mysql> SELECT chr.start, chr.end FROM chr
    -> JOIN chr start ON
    -> (chr.start = chr.end AND chr.end = chr.start)
    -> WHERE strand='-'
    -> SET (chr.start=inverse.2 AND chr.end=inverse.1);

Reply With Quote
  #2 (permalink)  
Old 05-25-11, 12:05
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
Have you tried using IF statements:

SELECT IF(col1="-",col3,col2) firstcol, IF(col1="-",col2,col3) secondcol
FROM table;

Like this:

mysql> select * from t1;
+------+------+------+
| col1 | col2 | col3 |
+------+------+------+
| + | 100 | 115 |
| - | 155 | 116 |
| + | 225 | 250 |
| - | 300 | 275 |
| + | 325 | 350 |
+------+------+------+
5 rows in set (0.00 sec)

mysql> select if(col1='-',col3,col2) first, if(col1='-',col2,col3) second
-> from t1;
+-------+--------+
| first | second |
+-------+--------+
| 100 | 115 |
| 116 | 155 |
| 225 | 250 |
| 275 | 300 |
| 325 | 350 |
+-------+--------+
5 rows in set (0.00 sec)
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #3 (permalink)  
Old 05-25-11, 12:34
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
ronan, he wanted to update the table and switch the values

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 05-25-11, 12:51
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
CREATE TABLE switcheroo
( col1 CHAR(1)
, col2 INTEGER
, col3 INTEGER
);
INSERT INTO switcheroo VALUES
 ('+',100,115)
,('-',155,116)
,('+',225,250)
,('-',300,275)
,('+',325,350)
;
UPDATE switcheroo 
   SET col2 = (@temp:=col2)
     , col2 = col3
     , col3 = @temp
 WHERE col1 = '-'
;
SELECT * FROM switcheroo
;
vwalah!!
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 05-25-11, 21:59
madkitty madkitty is offline
Registered User
 
Join Date: Apr 2011
Posts: 18
Code:
UPDATE switcheroo 
   SET col2 = (@temp:=col2)
     , col2 = col3
     , col3 = @temp
 WHERE col1 = '-'
WOW It works like a charm
Thanks a bunch
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