dbTalk Databases Forums  

Comparing columns from 2 tables, using a wildcard

comp.databases.mysql comp.databases.mysql


Discuss Comparing columns from 2 tables, using a wildcard in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Jason C
 
Posts: n/a

Default Comparing columns from 2 tables, using a wildcard - 12-09-2011 , 03:56 AM






I'm trying to select data from 2 separate tables, when one column is LIKE a column in the other table.

For example, I have tableA and tableB. col2 in tableA has a row with '"Jason"', and col2 in tableB has 'Jason' (without the double quotes), but I still want them to match.

This is what I thought would work, but doesn't (I just get a syntax error):

SELECT tableA.col1, tableB.col1
FROM tableA, tableB
WHERE tableA.col2 LIKE "%". tableB.col2 ."%"


Is there a correct way to use a wildcard here?

Reply With Quote
  #2  
Old   
Álvaro G. Vicario
 
Posts: n/a

Default Re: Comparing columns from 2 tables, using a wildcard - 12-09-2011 , 04:47 AM






El 09/12/2011 10:56, Jason C escribió/wrote:
Quote:
I'm trying to select data from 2 separate tables, when one column is LIKE a column in the other table.

For example, I have tableA and tableB. col2 in tableA has a row with '"Jason"', and col2 in tableB has 'Jason' (without the double quotes), but I still want them to match.

This is what I thought would work, but doesn't (I just get a syntax error):

SELECT tableA.col1, tableB.col1
FROM tableA, tableB
WHERE tableA.col2 LIKE "%". tableB.col2 ."%"


Is there a correct way to use a wildcard here?
The LIKE operator expects a string:

http://dev.mysql.com/doc/refman/5.5/...#operator_like

So you need to compose a string and «.» is *not* a string operator in
SQL dialect I'm aware of. Try this instead:

http://dev.mysql.com/doc/refman/5.5/...unction_concat

Beware of NULL values and you're done.


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

Reply With Quote
  #3  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: Comparing columns from 2 tables, using a wildcard - 12-09-2011 , 05:18 AM



On 2011-12-09 10:56, Jason C wrote:
Quote:
I'm trying to select data from 2 separate tables, when one column is LIKE a column in the other table.

For example, I have tableA and tableB. col2 in tableA has a row with '"Jason"', and col2 in tableB has 'Jason' (without the double quotes), but I still want them to match.

This is what I thought would work, but doesn't (I just get a syntax error):

SELECT tableA.col1, tableB.col1
FROM tableA, tableB
WHERE tableA.col2 LIKE "%". tableB.col2 ."%"


Is there a correct way to use a wildcard here?

select ta.col1 as ta_col1, tb.col1 as tb_col1
from ta
join tb
on tb.col1 like '%' || ta.col1 || '%';


/Lennart

Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.