tags:

views:

35

answers:

3

What do I do, I need to fetch data from 3 tables in mysql, here is my current query. All of the tables contain the IDNO which has 03A45 number. But this query isnt returning any results:

SELECT * 
  FROM father, mother, parents 
 WHERE father.IDNO=mother.IDNO=parents.IDNO 
   AND mother.IDNO='03A45' 
   AND father.IDNO='03A45' 
   AND parents.IDNO='03A45'

What would be the correct query for this?
All of the tables have the IDNO as primary key.

+3  A: 

Something like this should work:

select *
from
father 
inner join mother on father.IDNO = mother.IDNO
inner join parents on mother.IDNO = parents.IDNO
where
father.IDNO = '03a45'
kristian
You are including tables twice - don't do **select * from mother, father, parents INNER JOIN...**, just **select * from mother INNER JOIN...**
kristian
@user225269: You're mixing ANSI-89 join syntax (in your question), while kristian's answer is ANSI-92. You can't use both, and I recommend the ANSI-92 format.
OMG Ponies
+2  A: 

Use:

SELECT p.*,
       f.*,
       m.* 
  FROM PARENTS p
  JOIN FATHER f ON f.idno = p.idno
  JOIN MOTHER m ON m.idno = p.idno
 WHERE p.idno = '03A45'
OMG Ponies
A: 

The following code you've entered don't work because you entered a wrong sql command:

Dim sqlcom As MySqlCommand = New MySqlCommand("Select * from mother, father, parents INNER JOIN mother on father.IDNO = mother.IDNO, INNER JOIN parents on mother.IDNO = parents.IDNO WHERE father.IDNO='" & TextBox14.Text & "'", sqlcon)

The good query is the query give by Kristian

The principle on Inner Join is to join 2 tables:

The syntax is :

Select a.myFields, b.myFields
FROM myFirstTable as a INNER JOIN mySecondTable ON a.PrimaryKey = b.ForeignKey
INNER JOIN myThirdTable as c ON a.PrimaryKey = c.ForeighKey 'For 3Tables

It's just an example and you can use many others table.

Keep an eye on Sql courses. It's very powerful when you understand how it's working

Julien

Garcia Julien