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 > [Help Please!] Compare 2 tables 4 rows with condition

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-23-11, 05:39
madkitty madkitty is offline
Registered User
 
Join Date: Apr 2011
Posts: 18
Question [Help Please!] Compare 2 tables 4 rows with condition

I have two tables of integers in which I need to compare two columns as follow:
table_1
col1 | col2 | col3 |

table 2
col1 | col2 | col3 |

I need to copy the content of table_2.col3 INTO table_1.col3 If I have the following condition :
table_1.col1 >= table_2.col1
AND IF table_1.col2 <= table_2.col2

This is what I wrote on MySQL:


Code:
mysql> INSERT INTO table_1.col3 
-> SELECT * FROM table_2.col3 WHERE 
-> table_1.col1 >= table_2.col1 
-> AND table_1.col2 <= table_2.col2;

The 1st issue I have is ... This doesn't work at all.. it returns that table_1.col3 doesn't exist (even thou it does exit..)

The 2nd issue is that MySQL would compare line 01 to line 01, then line 02 to line 02 for each table. Optimally I need it to compare line 01 of table_1 to each line in table_2 and then, go to line 02, compare it to each line in table_2 and so on..Is it possible?
Reply With Quote
  #2 (permalink)  
Old 05-23-11, 07:30
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
UPDATE table_1 
INNER
  JOIN table_2
    ON table_2.col1 <= table_1.col1
   AND table_2.col2 >= table_1.col2
   SET table_1.col3 = table_2.col3
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 05-24-11, 04:13
madkitty madkitty is offline
Registered User
 
Join Date: Apr 2011
Posts: 18
Question General Question - Compare tables

Hi,

MySQL can compare row by row two different tables (let's say table1 and table2). Is it possible to make it compare row 01 from table1 to the EVERY rows in table2, then compare row02 from table1 to EVERY rows in table2 and so on...?
Reply With Quote
  #4 (permalink)  
Old 05-24-11, 04:19
madkitty madkitty is offline
Registered User
 
Join Date: Apr 2011
Posts: 18
Thanks but .. it doesn't work on my computer. It returns an error "Lock wait timeout exceeded"

This one works well, it displays on screen every matching element from table1.col3. Still I need to populate table1 from these elements.

Code:
mysql> SELECT table2.col3 FROM table1, table2
    -> WHERE (table1.col1<= table2.col1
    -> AND table1.col2>= table2.col2);
How?...I do not know
Reply With Quote
  #5 (permalink)  
Old 05-24-11, 04:36
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by madkitty View Post
WHERE table_1.col1 >= table_2.col1
AND table_1.col2 <= table_2.col2
Quote:
Originally Posted by madkitty View Post
WHERE (table1.col1 <= table2.col1
AND table1.col2 >= table2.col2)
can't make up your mind, can you

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 05-24-11, 04:38
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by madkitty View Post
Is it possible ...
of course it is possible
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 05-24-11, 09:14
madkitty madkitty is offline
Registered User
 
Join Date: Apr 2011
Posts: 18
haha Thanks & Sorry the 1st statement is the correct one

but still i can't get this to work on my computer.. The table1 is not updated, how can I fix this?

Also, do you know how can I compare each row in table1 to every rows in table2 ?
Reply With Quote
  #8 (permalink)  
Old 05-24-11, 09:15
madkitty madkitty is offline
Registered User
 
Join Date: Apr 2011
Posts: 18
Thanks Can you tell me quickly how can I do that?

I'm really clueless..
Reply With Quote
  #9 (permalink)  
Old 05-24-11, 12:46
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by madkitty View Post
Also, do you know how can I compare each row in table1 to every rows in table2 ?
use a CROSS JOIN
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 05-24-11, 12:47
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
use a CROSS JOIN
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #11 (permalink)  
Old 05-24-11, 17:02
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
threads merged
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #12 (permalink)  
Old 05-24-11, 18:23
madkitty madkitty is offline
Registered User
 
Join Date: Apr 2011
Posts: 18
Quote:
threads merged
I thought my eyesight was getting bad haha


Thanks a bunch r937, luv ya XD
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