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.ID开发者_运维知识库NO
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.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'
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'
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
精彩评论