简体   繁体   English

从两个表中的ID相同的两个表中选择数据

[英]Select Data from two tables where ID on both tables are same

Okay I have two tables called subobject : parentID, objectName, subID(primary) and subrelation : ID, className 好的,我有两个名为subobject的表:parentID,objectName,subID(primary)和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. 我希望将SubID与两个表中的ID匹配,具体取决于它们是否相同,然后迭代所有相同的值。 Whats the query to do this in Mysql. 什么是在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 我知道这与JOIN有关,这是我使用的mysql查询,但它不起作用

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

also my while loop to grab this 抓住这个也是我的while循环

while($join = mysqli_fetch_assoc($join))

JOIN the two tables: JOIN两个表:

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

See it in action here: 在这里看到它:


Also, see the following post for more info about the different types of JOIN s: 另外,有关不同类型的JOIN的更多信息,请参阅以下帖子:

select 
  a.objectName, b.className 
from 
  subobject a 
left join 
   subrelation b on a.subID = b.ID

Use a Join 使用Join

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

You can find information on SQL Joins here: http://en.wikipedia.org/wiki/Join_(SQL) 您可以在此处找到有关SQL连接的信息: http//en.wikipedia.org/wiki/Join_( SQL

And information from the MySQL manual on Joins: http://dev.mysql.com/doc/refman/5.0/en/join.html 以及关于联接的MySQL手册中的信息: http//dev.mysql.com/doc/refman/5.0/en/join.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM