473,403 Members | 2,359 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,403 software developers and data experts.

Joining three tables... getting 100 rows where there should only be six.

I am trying to write a query that joins 3 tables and having a
problem.... First, some structure info, the 3 tables are.....

Expand|Select|Wrap|Line Numbers
  1. Address
  2.      Name
  3.      Street
  4.      City
  5.      State
  6.      Zipcode
  7.      ID
  8.  
  9. Contact
  10.      Name
  11.      Phone
  12.      Fax
  13.      Email
  14.      URL
  15.  
  16. Keys
  17.      Name
  18.      Keywords
  19.      ID
  20.  
Now, I get a term from a form and search Keys for matches in Keywords.

I take the Name value and go to the other tables for info based on
that name. For example to display all of the info for 'Antiques' I
use Keys to get the names of all of the antique dealers. I then need
go to Address and get the address info and Contact to get the contact
info.

I tried using
Expand|Select|Wrap|Line Numbers
  1. SELECT Cats.Name,Address.Address,Address.City
  2.                FROM Cats, Address
  3.                 where Cats.Category LIKE '%%$search%%'
But that didn't work out. It gave me one name and well over 100
entries when I selected 'accountants' from the form when I know that
there are 6 accountants in the DB.

HELP!

Thanks for reading,
Jim
Oct 13 '07 #1
2 1627
pbmods
5,821 Expert 4TB
Heya, Jim.

Changed thread title to better describe the problem (did you know that threads whose titles do not follow the Posting Guidelines actually get FEWER responses?).

You're joining three tables (well, two in the example you provided), but you're not giving MySQL any criteria for the join, so you are literally seeing every row of every table joined together in every possible combination that fulfills the WHERE clause.

Try this instead:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         `Cats`.`Name`,
  3.         `Address`.`Address`,
  4.         `Address`.`City`
  5.     FROM
  6.     (
  7.             `Cats`
  8.         LEFT JOIN
  9.             `Address`
  10.                 USING (`Name`)
  11.     )
  12.     WHERE
  13.         `Cats`.`Category` LIKE '%$search%'
  14.  
Oct 13 '07 #2
Oops, I left the contact info out of the original query. I figured that one out on my own!

Your suggestion works perfectly.

Thanks for the help!
Jim
Oct 13 '07 #3

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

Similar topics

4
by: Job Lot | last post by:
Is there anyway of Joining two or more DataTable with similar structure? I have three DataTables with following structures Data, AmountB/F, Repayments, InterestCharged and AmountC/F i want...
3
by: james | last post by:
Hi, I would like to get all the records from 9 tables that have the same field value in one field (it is a unique field)that is shared by all the tables. Would this method of joining work: ...
3
by: Matt. | last post by:
Hi all! The SQL is below. Here is the scenario. The table Production contains all of the production information for our plant. Primary key is Job #, ProDate, Shift, Machine #. The table...
7
by: RLN | last post by:
Re: Access 2000 I have three history tables. Each table contains 3 years worth of data. All three tables have a date field in them (and autonum field). Each table has the potential to contain...
2
by: Thomas R. Hummel | last post by:
Hello, I am currently working on a monthly load process with a datamart. I originally designed the tables in a normalized fashion with the idea that I would denormalize as needed once I got an...
11
by: MP | last post by:
context: (vb6 / ado / .mdb / jet4.0 / not using access) hypothetical problem say I need to track the properties of boxes. There are three kinds, cardboard, wood, and sheet metal. Each box...
3
by: BrianDH | last post by:
Hi I have a form with multi-tabs, and on each tab I have a one combo that has the same values. I populate all 3 combo with the same one datacall/dataset My problem is: when I make a value...
2
by: me | last post by:
I have written a query that joins four or five tables. One table has 30,000 rows. Another table has only 200. I want to only return the 200 or so rows in the smaller table and columns from the...
1
by: aknoch | last post by:
My basic situation is this - I ONLY want duplicates, so the opposite of DISTINCT: I have two tables. Ordinarily, Table1ColumnA corresponds in a one to one ratio with Table2ColumnB through a...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.