Current location: Hot Scripts Forums » Other Discussions » Database » Column name in LIKE query


Column name in LIKE query

Reply
  #1 (permalink)  
Old 06-19-08, 10:03 PM
dave111 dave111 is offline
Wannabe Coder
 
Join Date: Jul 2003
Posts: 136
Thanks: 0
Thanked 0 Times in 0 Posts
Column name in LIKE query

Does anyone know how to use the mysql LIKE function with a column name.

What i'm trying to do is something like this:

Code:
SELECT * FROM `tablename` WHERE `columna` LIKE '%`columnb`%'
But this doesn't seem to work, any ideas?
Reply With Quote
  #2 (permalink)  
Old 06-22-08, 12:46 AM
tkangel tkangel is offline
New Member
 
Join Date: Jun 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
You can use the following:

Code:
show columns from 'tablename'
where field like '%columnb%';

Last edited by Nico; 06-22-08 at 03:59 AM. Reason: Removed URL in fake signature.
Reply With Quote
  #3 (permalink)  
Old 06-22-08, 08:31 AM
dave111 dave111 is offline
Wannabe Coder
 
Join Date: Jul 2003
Posts: 136
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by tkangel View Post
You can use the following:

Code:
show columns from 'tablename'
where field like '%columnb%';
That is not the same thing at all.
Your query would display columns which have a name like "columnb"

I want to display rows where the data in "columna" is like the data in "columnb".
Reply With Quote
  #4 (permalink)  
Old 06-24-08, 08:45 AM
sandeep.kumar's Avatar
sandeep.kumar sandeep.kumar is offline
Wannabe Coder
 
Join Date: Jun 2008
Location: India
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
may be this code will work

Code:
SELECT * FROM tablename as t1, tablename as t2  WHERE  t1.columna =  t2.columnb;
Reply With Quote
  #5 (permalink)  
Old 06-25-08, 06:09 AM
dave111 dave111 is offline
Wannabe Coder
 
Join Date: Jul 2003
Posts: 136
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by sandeep.kumar View Post
may be this code will work

Code:
SELECT * FROM tablename as t1, tablename as t2  WHERE  t1.columna =  t2.columnb;
This is incorrect:
1) - "columna" and "columnb" need to be in the same table (not 2 different tables)

2) - I'm asking about LIKE not EQUAL

Does no one know the answer to this?
Reply With Quote
  #6 (permalink)  
Old 06-25-08, 06:20 AM
sandeep.kumar's Avatar
sandeep.kumar sandeep.kumar is offline
Wannabe Coder
 
Join Date: Jun 2008
Location: India
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
In my query m using same table.

Tablename = 'your_table'

Code:
SELECT * FROM your_table as t1, your_table as t2  WHERE  t1.columna =  t2.columnb;

I'm just using SELF JOIN.
Reply With Quote
  #7 (permalink)  
Old 06-25-08, 11:11 AM
dave111 dave111 is offline
Wannabe Coder
 
Join Date: Jul 2003
Posts: 136
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by sandeep.kumar View Post
In my query m using same table.

Tablename = 'your_table'

Code:
SELECT * FROM your_table as t1, your_table as t2  WHERE  t1.columna =  t2.columnb;

I'm just using SELF JOIN.
Ah, i see; it still does not work with the LIKE function though:
Code:
SELECT * FROM your_table as t1, your_table as t2  WHERE  t1.columna LIKE   '%t2.columnb%';
Reply With Quote
  #8 (permalink)  
Old 07-10-08, 03:44 AM
dustin56 dustin56 is offline
Newbie Coder
 
Join Date: Jun 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
I found someone through Google with a similar issue. Here is an adaptation of the code that they used successfully.
Code:
SELECT * FROM tablename WHERE columna LIKE concat('%', columnb, '%')
Apparently you have to concatenate the wildcards into the string before LIKE can use them in a comparison. This makes perfect sense when you consider that LIKE expects to compare literal strings. Definitely not trivial though. I had to dig around a bit to find this solution.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Firefox and align problems gigafare CSS 12 01-07-07 11:22 AM
Overlapping table cells in IE, table/css three column layout (WebTV info also wanted) TwoD CSS 4 11-09-06 07:39 PM
I most definately suggest DevelopingCentral.com For Any Website Design/Development! Salty777 General Advertisements 2 10-01-04 04:27 AM
Declared Functions skipper23 PHP 4 12-17-03 10:06 AM
index page not showing up skipper23 PHP 3 12-15-03 01:10 PM


All times are GMT -5. The time now is 05:03 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.