简体   繁体   English

比较两个具有相同列的表的相似值

[英]compare two tables with same column for similar values

I have two tables tableA and tableB . 我有两个表tableAtableB Both have two similar columns ID and B_ID . 两者都有两个相似的列IDB_ID I want to check whether both table have similar values. 我想检查两个表是否具有相似的值。 My code is: 我的代码是:

$ac = $mysql->query("(SELECT ID,B_ID FROM tableA) INTERSECT (SELECT ID,B_ID FROM tableB)");

  if($ac){
    while($row  = $ac->fetch_assoc()){

      echo "ID ".$row["ID"]." B_ID".$row["B_ID"]."<br>";
    }
  }

But this doesn't give any result. 但这没有任何结果。

    ps: tableA(ID,B_ID)

 1->23
 2->23
 3->23
 4->56
 5->67

tableB(ID,B_ID)

3->23
8->26
11->27
12->66

here both table has 3->23 but 1->23 2->23 is not in tableB how can i figure that sort of records. 这两个表都具有3-> 23,但1-> 23 2-> 23不在tableB中,我该如何计算这种记录。 same B_ID but different ID 相同的B_ID,但ID不同

If you have B.ID column is present in both table then use following JOIN query 如果两个表中都有B.ID列,则使用以下JOIN查询

SELECT a.ID, a.B_ID 
FROM tableA AS a
JOIN tableB AS b ON (a.B_ID = b.B_ID AND a.ID = b.ID)

Use a join to get the data and just iterate over your results. 使用联接获取数据并仅遍历结果。 Run the query and if there is any record that satisfies the query or not. 运行查询,以及是否有任何记录满足查询条件。

select t1.ID, t1.B_ID from tableA t1, tableB t2
where t1.ID = t2.ID
and   t1.B_ID =t2.B_ID  

Use an INNER JOIN and COUNT 使用内INNER JOINCOUNT

SELECT COUNT(*) as cnt
FROM tableA INNER JOIN tableB
ON tableA.B_ID = tableB.B_ID AND tableA.ID = tableB.ID

now if cnt is greater than zero than common values exist, otherwise no 现在,如果cnt大于零,则存在公共值,否则不存在

Please try with this query may be help full. 请尝试使用此查询可能会对您有所帮助。

SELECT
    tbla.ID,
    tbla.B_ID
FROM
    tablea AS tbla,
    tableb AS tblb
WHERE
    tbla.B_ID = tblb.B_ID

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

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