5

Okay I have two tables called subobject: parentID, objectName, subID(primary) and subrelation: ID, className

parentID | objectName | subID            ID| className|
_____________________________            ______________
    84   |   Test     |   14             14|    BOM      
    84   |   Test2    |   15             15|    Schematics

I want to match SubID with ID from both tables depending if they are the same values, then iterate all the values that are the same. Whats the query to do this in Mysql.

this is how I want it to look:

subobjectNAME:
     --RelatedClass
     --RelatedClass2

etc.

I know this is has something to do with JOIN and this is the mysql Query im using but its not working

"SELECT * from subrelation inner join subobject on subrelation.ID = subobject.subID"

also my while loop to grab this

while($join = mysqli_fetch_assoc($join))
4

3 回答 3

7

JOIN两张表:

SELECT
  so.objectName,
  sr.ClassName
FROM subobject AS so
INNER JOIN subrelation AS sr ON so.subId = sr.ID;

在这里查看它的实际效果:


JOIN此外,有关不同类型的s的更多信息,请参阅以下帖子:

于 2013-05-24T15:05:21.413 回答
0
select 
  a.objectName, b.className 
from 
  subobject a 
left join 
   subrelation b on a.subID = b.ID
于 2013-05-24T15:06:52.287 回答
0

用一个Join

SELECT 
    subobject.ObjectName, 
    subrelation.ClassName 
FROM 
    subobject 
INNER JOIN
    subrelation ON subobject.subID = subrelation.ID

您可以在此处找到有关 SQL 连接的信息: http://en.wikipedia.org/wiki/Join_(SQL)

以及关于加入的 MySQL 手册中的信息:http: //dev.mysql.com/doc/refman/5.0/en/join.html

于 2013-05-24T15:07:19.853 回答