473,395 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Comparing two varchar fields from separate tables

12
hi,

how do i compare two varchar fields?

my table has two fields, name and description. i need to find those records where the name does not match with or exist in the description field anywhere. should i use a "not exists"?

thanks,
s
Aug 13 '07 #1
6 7241
pbmods
5,821 Expert 4TB
Heya, S.

I'm not sure I understand what you are trying to accomplish. Do you need to check to see if one of the fields occurs somewhere in the second field?

P.S., Changed thread title to better describe the problem (did you know that threads whose titles contain three words or less actually get FEWER responses?).
Aug 13 '07 #2
srathi
12
thanks pbmods.

so i have this table, description_tbl that has the fields: name and description. I'd like to compare the two fields and see if the 'value' in the name field exists anywhere in the description field.

so if the name is Microsoft and the desc field has something like "Inc. provides...." it should a part of my results because microsoft does not exist in the description field.

both the name and description fields are varchar.

hope this makes sense.

p.s. how do i edit the title thread?





Heya, S.

I'm not sure I understand what you are trying to accomplish. Do you need to check to see if one of the fields occurs somewhere in the second field?

P.S., Changed thread title to better describe the problem (did you know that threads whose titles contain three words or less actually get FEWER responses?).
Aug 13 '07 #3
pbmods
5,821 Expert 4TB
Heya, S.

To answer your second question first, you can't. The moderators on this site are well-versed in what makes a good thread title, and if we feel that a thread title needs editing, we'll just take care of it. However, we prefer it when thread titles do not require editing in the first place.

I encourage you to look over our Posting Guidelines, especially the part about Thread Titles. That'll put you ahead of roughly 85% of all posters with under 100 posts on this site :)

Ok then. Now onto your problem.

Normally, this would be a simple solution; simply use a LIKE subclause in the WHERE clause:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         *
  3.     FROM
  4.         `description_tbl`
  5.     WHERE
  6.         `description` LIKE '%Microsoft%';
  7.  
The tricky part comes in because we don't know the exact text that we are searching for (depends on the value of the other field).

Fortunately, MySQL has a function that handles this situation very nicely:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         *
  3.     FROM
  4.         `description_tbl`
  5.     WHERE
  6.         `description` LIKE CONCAT('%', `name`, '%');
  7.  
Word of warning: Because MySQL has to calculate the value of CONCAT(), this search is not fully indexable! Be prepared for slow query times on large data sets!
Aug 13 '07 #4
srathi
12
hey pbmods,

i'll def keep the post guidelines in mind going forward. and thanks for looking into my query...

now, there's a slight problem witht concat. the sql version i'm using does not support this function.

also is there a way i can remove characters and spaces from the fields i'm comparing? basically, i'd like to see if "ABB" (probably use the Len function to pick a part of the "name field" ) exists anywhere in the "Description" field because ABB can occur anywhere in the description field.

example:
name: ABB Ltd.
description: ABB, Ltd. provides power and...
(if i can remove all spaces and characters both the fields, then ABBLtd can be compared with the descriptions ABBLtd.

Thanks again for checking this out...
S
Aug 13 '07 #5
pbmods
5,821 Expert 4TB
Heya, S.

If your MySQL version doesn't support CONCAT(), then this probably won't be very helpful, either, but...

You might want to look into fulltext searching.

If neither CONCAT() nor fulltext are an option, you may need to load the entire table into another script, such as PHP, and then check for matches that way. Note that if you do this, you might want to cache the results in the table so that you don't have to do this every time the User loads the page!
Aug 13 '07 #6
srathi
12
thanks! i'll try that.

Heya, S.

If your MySQL version doesn't support CONCAT(), then this probably won't be very helpful, either, but...

You might want to look into fulltext searching.

If neither CONCAT() nor fulltext are an option, you may need to load the entire table into another script, such as PHP, and then check for matches that way. Note that if you do this, you might want to cache the results in the table so that you don't have to do this every time the User loads the page!
Aug 13 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Rick Caborn | last post by:
Does anyone know of a way to execute sql code from a dynamically built text field? Before beginning, let me state that I know this db architecture is built solely for frustration and I hope to...
3
by: twdo | last post by:
Let me see if I can explain my situation clearly. I have a table with the columns: answer_id, question_id, member_id, answer - answer_id is the primary key for the table. - question_id...
21
by: Scott Marquardt | last post by:
What are some good strategic approaches to using freeform text fields for data that needs to be queried? We have a product whose tables we can't change, and I need to count on a "description" field...
5
by: Megan | last post by:
Hi everybody- I'm helping a friend with a music database. She has an old one and is creating a new one. She wants to compare records and fields in the old database with records and fields in the...
4
by: osmethod | last post by:
Hello, I have read many articles about comparing tables, like - loops, delete queries, appending to temp table with index etc Problem: 2 tables T1 & T2. Data is suppposed to be the same in...
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
21
by: Kristaps | last post by:
Hi everyone! I have some questions, maybe someone can help me... I write script for table comparing, but it works wery slovly. There is the script, can anyone give some tip how can I make this...
0
by: maheshmohta | last post by:
Background Often while remodeling legacy application, one of the important tasks for the architects is to have an optimum usage of storage capabilities of database. Most of the legacy applications...
4
by: gillianbrooks91 | last post by:
Forgive me for asking this question, I've trawled through nearly every available post on this subject that I can find for a few weeks now but nothing quite points me in the right direction. I'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.