从mysql中的3个表中读取问题

时间:2010-03-18 04:01:00

标签: sql mysql

我该怎么做,我需要从mysql中的3个表中获取数据,这是我当前的查询。所有表都包含IDNO,其编号为03A45。但是这个查询不会返回任何结果:

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

对此有什么正确的查询?
所有表都将IDNO作为主键。

3 个答案:

答案 0 :(得分:3)

这样的事情应该有效:

select *
from
father 
inner join mother on father.IDNO = mother.IDNO
inner join parents on mother.IDNO = parents.IDNO
where
father.IDNO = '03a45'

答案 1 :(得分:2)

使用:

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'

答案 2 :(得分:0)

您输入的以下代码无效,因为您输入了错误的sql命令:

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)

好的查询是克里斯蒂安提出的查询

Inner Join的原则是加入2个表:

语法为:

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

这只是一个例子,您可以使用许多其他表。

密切关注Sql课程。当你理解它是如何工作的时候它非常强大

于连