简体   繁体   English

如果行匹配,如何比较来自不同表的两行并显示表值?

[英]How to compare two rows from different tables and show the table values if the rows match?

I have two different tables. 我有两个不同的表。 One is person and the other one is Cardholder . 一个是person ,另一个是Cardholder Both of these tables have one row with the similar name and same value and its called personId . 这两个表都有一行具有相似名称和相同值的行,其名称为personId What I am trying to do is compare the row personId on the person table to personId on the Cardholder table and print only rows that match the value of personId on both tables. 我想做的是将person表上的personId行与持卡人表上的personId比较,并仅在两个表上打印与personId值匹配的行。 How would I write in PHP to do such task? 我将如何用PHP编写以完成此类任务? I tried few things but failed to do what I intend. 我尝试了几件事,但未达到我的预期。

The sql query would look something like this : sql查询看起来像这样:

SELECT P.* FROM person P INNER JOIN Cardholder C ON C.personId=P.personId 从个人P内联接中选择P. *持卡人C ON C.personId = P.personId

this would only select the fields from the table person that has a match on personId in the Cardholder table. 这样只会从持卡人表中personId匹配的表person中选择字段。

You will need code like this one 您将需要像这样的代码

MySQL Code: - MySQL代码:-

("SELECT Cardholder.personId, person.personId FROM Cardholder INNER JOIN person ON Cardholder.personId = person.personId");

PHP Code: - PHP代码:-

if (count($SQLstatment) > 0) {Exist} else {Not}

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

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