繁体   English   中英

mysql如何比较两个表?

[英]How to compare two tables mysql?

我有三个表:

表1称为“卡片”。 重要的字段是:

"state_id" - This table has 3 values (1,2,3)
"associated_driver" - called "referred_as" on driver table
"referred_as" - called "associated_card" on driver table

表2称为“驱动程序”。 重要的字段是:

"ID" - The auto incremented value of the table
"associated_card" - Has a value , normally some number e.g 123555
"referred_as" - The name of the driver () called "associated_driver" on card table

忘记添加此表:

表3称为“ card_driver”。 重要的字段是:

"driver_id" - The id from the driver table that links to the card
"card_id" - The id from the card table that links to the driver

我想发生的事情:

当用户从驱动程序表中输入其ID时,它将比较两个表都具有的字段,即“关联卡”字段。 从那里开始,来自(bellow)的原始查询将被接管,尽管也需要进行修改。

我真的不知道如何执行sql查询。

我需要更改此部分:

$sql ="SELECT * FROM card WHERE id = '$id'" ;

谢谢

原始代码:

$id = $_POST['id'];

$sql ="SELECT * FROM card WHERE id = '$id'" ;
mysql_select_db('damp');
$result = mysql_query( $sql, $conn );
$row = mysql_fetch_assoc($result);
switch($row['state_id'])
{
case "1":
   echo "<strong><font color=\"green\">Authorisation Granted!</font></strong>";
   break;
case "2":
   echo "<strong><font color=\"red\">Your card has expired and authorisation is denied</font></strong>";
   break;
case "3":
   echo "<strong><font color=\"red\">Your card has been cancelled and authorisation is denied</font></strong>";
   break;
default:
   echo "<strong><font color=\"red\">The Card ID does not exist</font></strong>";
}

//echo $_GET['here']; // Prints the name of the page content
//echo $class_obj_id; // Prints the id number of the selected row
?>
<?php } ?>
<div id="auth">
<form method="post" action="<?php $_PHP_SELF ?>">

<table id="auth" width="150" >
<tr>
    <td colspan="2"><h2>Check Card Authorisation</h2></td>
   </tr>
<tr>
<td width="50" align="center">ID</td>
<td><input name="id" type="text" id="id" value="" /></td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="check" type="submit" id="check" value="Check">
</td>
</tr>
</table>
</form>
</div>
$sql ="SELECT * FROM card c JOIN driver d ON c.referred_as=d.referred_as WHERE d.ID='$id'";
$sql = "SELECT * FROM driver INNER JOIN card ON driver.associated_card = card.associated_card WHERE id = '$id'";

加入驱动程序和卡片表,其中相关的卡片ID匹配,然后您可以从此调用$row['state_id']

暂无
暂无

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

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