473,388 Members | 1,220 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,388 software developers and data experts.

Inner join example

Hi all,

I have a relational database, where all info is kept in separate tables
and just the id's from those tables are stored in one central table
(tblMaster)...
I want to perform a query, so all data from the separate tables is shown
in a view (instead of the reference id's pointing to the separate tables...)
I have some troubles formulating the SQL statement:
I tried:

SELECT tblMaster.*, tblHelper1.*, tblHelper2.*
FROM (tblMaster INNER JOIN tblHelper1 ON
tblMaster.to_idHelper1=tblHelper1.id) INNER JOIN tblHelper2 ON
tblMaster.to_idHelper2=tblHelper2.id;

But I don't get any results...

Any suggestions?

Thanks in advance,

Zeff
Apr 11 '07 #1
3 16480
Your query will select all records from these three tables where a
specific Master record has both a helper1 and helper2.

What does this query return?

SELECT *
FROM tblMaster
WHERE to_idHelper1 is not null and to_idHelper2 is not null

If this query returns no records then your issue is that you aren't
doing what you want to do.

Otherwise, what are the structures of the tables? Are the fields
included in the joins of the same data type?

Cheers,
Jason Lepack

On Apr 11, 10:03 am, Zeff <z...@trash.netwrote:
Hi all,

I have a relational database, where all info is kept in separate tables
and just the id's from those tables are stored in one central table
(tblMaster)...
I want to perform a query, so all data from the separate tables is shown
in a view (instead of the reference id's pointing to the separate tables...)
I have some troubles formulating the SQL statement:
I tried:

SELECT tblMaster.*, tblHelper1.*, tblHelper2.*
FROM (tblMaster INNER JOIN tblHelper1 ON
tblMaster.to_idHelper1=tblHelper1.id) INNER JOIN tblHelper2 ON
tblMaster.to_idHelper2=tblHelper2.id;

But I don't get any results...

Any suggestions?

Thanks in advance,

Zeff

Apr 11 '07 #2
Dear Jason,

Thanks for your reply. Inner joins show only corresponding records in
all tables...

I found that this shows all coresponding rows:
SELECT tblMaster.*, tbl1.naam1, tbl2.naam2, tbl3.naam3
FROM tbl3 INNER JOIN (tbl2 INNER JOIN (tbl1 INNER JOIN tblMaster ON
tbl1.id_table1=tblMaster.ref_tbl1) ON tbl2.id_table2=tblMaster.ref_tbl2)
ON tbl3.id_table3=tblMaster.ref_tbl3;

Table schemes:
tblMaster(master_id,date,ref_tbl1,ref_tbl2, ref_tbl3)
tbl1(id_tbl1, naam1)
tbl2(id_tbl2, naam2)
tbl3(id_tbl3, naam3)

Can you help me with the syntax for LEFT JOINS, to display also non
corresponding records from tblMaster?

Many thanks,

Zeff

Jason Lepack wrote:
Your query will select all records from these three tables where a
specific Master record has both a helper1 and helper2.

What does this query return?

SELECT *
FROM tblMaster
WHERE to_idHelper1 is not null and to_idHelper2 is not null

If this query returns no records then your issue is that you aren't
doing what you want to do.

Otherwise, what are the structures of the tables? Are the fields
included in the joins of the same data type?

Cheers,
Jason Lepack

On Apr 11, 10:03 am, Zeff <z...@trash.netwrote:
>Hi all,

I have a relational database, where all info is kept in separate tables
and just the id's from those tables are stored in one central table
(tblMaster)...
I want to perform a query, so all data from the separate tables is shown
in a view (instead of the reference id's pointing to the separate tables...)
I have some troubles formulating the SQL statement:
I tried:

SELECT tblMaster.*, tblHelper1.*, tblHelper2.*
FROM (tblMaster INNER JOIN tblHelper1 ON
tblMaster.to_idHelper1=tblHelper1.id) INNER JOIN tblHelper2 ON
tblMaster.to_idHelper2=tblHelper2.id;

But I don't get any results...

Any suggestions?

Thanks in advance,

Zeff

Apr 12 '07 #3
What exactly are you trying to accomplish and why must it be done this
way?

You have a many to many relationship, so I would store the data like
this:

(stores the master info)
tbl_master:
master_id
master_date

(stores the name info)
tbl_names:
name_id
name_text

(links a name to a master)
tbl_master_names:
master_id
name_id

The best way I can think to do it if you can't change your design is
either with nesting subqueries or 3 queries (both work in the same
way).

Create this query and call it step1
SELECT tblMaster.*, tbl1.naam1
FROM tbl1 RIGHT JOIN tblMaster ON tbl1.id_table1 = tblMaster.ref_tbl1;

Create this query and call it step2
SELECT step1.*, tbl2.naam2
FROM step1 LEFT JOIN tbl2 ON step1.ref_tbl2 = tbl2.id_table2;

This is your final result
SELECT step2.*, tbl3.naam3
FROM step2 LEFT JOIN tbl3 ON step2.ref_tbl3 = tbl3.id_table3;

As one big conglomerate query it looks like this:
SELECT step2.*,
tbl3.naam3
FROM (SELECT step1.*,
tbl2.naam2
FROM (SELECT tblMaster.*,
tbl1.naam1
FROM tbl1
RIGHT JOIN tblMaster
ON tbl1.id_table1 = tblMaster.ref_tbl1) AS
step1
LEFT JOIN tbl2
ON step1.ref_tbl2 = tbl2.id_table2) AS step2
LEFT JOIN tbl3
ON step2.ref_tbl3 = tbl3.id_table3;

That will perform what you want, though I think it would be better to
revisit your design.

Cheers,
Jason Lepack

On Apr 12, 3:08 am, Zeff <z...@trash.netwrote:
Dear Jason,

Thanks for your reply. Inner joins show only corresponding records in
all tables...

I found that this shows all coresponding rows:
SELECT tblMaster.*, tbl1.naam1, tbl2.naam2, tbl3.naam3
FROM tbl3 INNER JOIN (tbl2 INNER JOIN (tbl1 INNER JOIN tblMaster ON
tbl1.id_table1=tblMaster.ref_tbl1) ON tbl2.id_table2=tblMaster.ref_tbl2)
ON tbl3.id_table3=tblMaster.ref_tbl3;

Table schemes:
tblMaster(master_id,date,ref_tbl1,ref_tbl2, ref_tbl3)
tbl1(id_tbl1, naam1)
tbl2(id_tbl2, naam2)
tbl3(id_tbl3, naam3)

Can you help me with the syntax for LEFT JOINS, to display also non
corresponding records from tblMaster?

Many thanks,

Zeff

Jason Lepack wrote:
Your query will select all records from these three tables where a
specific Master record has both a helper1 and helper2.
What does this query return?
SELECT *
FROM tblMaster
WHERE to_idHelper1 is not null and to_idHelper2 is not null
If this query returns no records then your issue is that you aren't
doing what you want to do.
Otherwise, what are the structures of the tables? Are the fields
included in the joins of the same data type?
Cheers,
Jason Lepack
On Apr 11, 10:03 am, Zeff <z...@trash.netwrote:
Hi all,
I have a relational database, where all info is kept in separate tables
and just the id's from those tables are stored in one central table
(tblMaster)...
I want to perform a query, so all data from the separate tables is shown
in a view (instead of the reference id's pointing to the separate tables...)
I have some troubles formulating the SQL statement:
I tried:
SELECT tblMaster.*, tblHelper1.*, tblHelper2.*
FROM (tblMaster INNER JOIN tblHelper1 ON
tblMaster.to_idHelper1=tblHelper1.id) INNER JOIN tblHelper2 ON
tblMaster.to_idHelper2=tblHelper2.id;
But I don't get any results...
Any suggestions?
Thanks in advance,
Zeff- Hide quoted text -

- Show quoted text -

Apr 12 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ike | last post by:
Oh I have a nasty query which runs incredibly slowly. I am running MySQL 4.0.20-standard. Thus, in trying to expedite the query, I am trying to set indexes in my tables. My query requires four...
3
by: Prem | last post by:
Hi, I am having many problems with inner join. my first problem is : 1) I want to know the precedance while evaluating query with multiple joins. eg. select Employees.FirstName,...
1
by: cbielins | last post by:
I'm trying to join a dimension table to a summary table using an inner join. However, I would like to join using a udf that I've created. Example: select ... from fact_table fact inner join...
6
by: dmonroe | last post by:
hi group -- Im having a nested inner join problem with an Access SQl statement/Query design. Im running the query from ASP and not usng the access interface at all. Here's the tables: ...
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
12
by: Chamnap | last post by:
Hello, everyone I have one question about the standard join and inner join, which one is faster and more reliable? Can you recommend me to use? Please, explain me... Thanks Chamnap
14
by: cjakeman | last post by:
Hi, Solved a little mystery yesterday when I built a form that combined 2 tables with a 1:M relationship and relational integrity. All the correct data was visible on the form but, if I tried to...
0
by: katupilar | last post by:
I am writing a tool to interface with a couple of tables in SQL Server 2000. I usually write my queries with an Inner Join to bring in fields from seperate tables and load that to a DataSet. I'm...
11
by: YZXIA | last post by:
Is there any difference between explicit inner join and implicit inner join Example of an explicit inner join: SELECT * FROM employee INNER JOIN department ON employee.DepartmentID =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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:
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
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
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...

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.