473,466 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Query for related tables

bhcob1
19 New Member
I have 2 tables, related between the fields 'field1' and 'field2'
tbl1
- field1
- field2
tbl2
- field3
- field4

I want to run a query so that it displays 4 columns (1 for each field), and displays all the records from tbl1 and if that record is related to a record in tbl2 to display the remaining 2 fields. Else, if there is no related record in tbl2, to leave the last 2 columns blank

The problem is, the query i get will only display records in tbl1 that have a related record in tbl2.

EXCUSE THE TERRIBLE EXAMPLE OF THE PROBLEM

tbl1
field1 field2
cat 1
dog 3
mouse 4
horse 5

tbl2
field3 field4
cat meow
horse nahh

What i would like to query to output would be
cat 1 cat meow
dog 3
mouse 4
horse 5 horse nahh

All i can get is
cat 1 cat meow
horse 5 horse nahh

Any suggestions.
Feb 13 '07 #1
4 1571
Rabbit
12,516 Recognized Expert Moderator MVP
Do a left join. This will include all records in tbl1 regardless of whether or not there's a matching record in table 2.
Feb 13 '07 #2
NeoPa
32,556 Recognized Expert Moderator MVP
In your example it would be :
Expand|Select|Wrap|Line Numbers
  1. SELECT tbl1.Field1,
  2.        tbl1.Field2,
  3.        tbl2.Field3,
  4.        tbl2.Field4
  5. FROM tbl1 LEFT JOIN tbl2
  6.   ON tbl1.Field1=tbl2.Field3
Feb 13 '07 #3
bhcob1
19 New Member
Thanks guys, LEFT JOIN worked great. Just what I was looking for
Feb 13 '07 #4
NeoPa
32,556 Recognized Expert Moderator MVP
Pleased to hear it - We aim to please :)
Feb 13 '07 #5

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

Similar topics

6
by: Xenophobe | last post by:
I know this isn't a MySQL forum, but my question is related to a PHP project. I have two tables. table1 table2 "table1" contains 2 columns, ID and FirstName:
14
by:  | last post by:
having a spot of trouble writing this one. if you are so inclined and have a moment, i'd really appreciate your insight. i have what amounts to a purchase order type of setup...a descriptive...
2
by: Fendi Baba | last post by:
I created a person table with various fields such as Suffix, Salutation, etc, Some of these fields may not be mandatory for example suffix. In the actual table itself, I only have a field for...
3
by: dwightsmail | last post by:
hi all, can anyone help me? I am a relative newbie to sql server and I am more familiar with Enterprise Manager than QA. I have made many many access databases though. I am making an asp.net...
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: jpr | last post by:
Hello, My database has 5 tables. WHen I add data to one table, it runs an append query that copies three records to other 4 tables. The main table is MASTER. The data I copy are: ID, SSN and...
5
by: themastertaylor | last post by:
I've got a system to manage various quotes for building materials for a number of sites. i want a query to produce a report that shows me who HASN'T quoted for which sites. basically so i can...
9
by: mansoorm | last post by:
I've a db containing two tables which every item in table one is linked to 1--200 items in table 2. I want to generate a report like this: Item in table 1...
2
by: existential.philosophy | last post by:
This is a new problem for me: I have some queries that open very slowly in design view. My benchmark query takes about 20 minutes to open in design view. That same query takes about 20 minutes...
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
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
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.