如何根据MySql中的一个属性比较两个不同行数的数据库表?

How to compare two DB tables of different number of rows based on one attribute in MySql?

我有:

1.I 有两个数据库 tables displaybid 如下所列:

mysql> select * from display where gp_no = 1;

+-------+--------------+-----------+--------------------+---------------+-----------+----------------+
| gp_no | no_of_member | amount    | current_instalment | starting_date | member_no | member_name    |
+-------+--------------+-----------+--------------------+---------------+-----------+----------------+
|     1 |           15 | 375000.00 |                  2 | 2015-05-01    |         1 | Akansha Gupta  |
|     1 |           15 | 375000.00 |                  2 | 2015-05-01    |         2 | Akash Bansal   |
|     1 |           15 | 375000.00 |                  2 | 2015-05-01    |         3 | Ashish Gupta   |
|     1 |           15 | 375000.00 |                  2 | 2015-05-01    |         4 | Chavi Jain     |
|     1 |           15 | 375000.00 |                  2 | 2015-05-01    |         5 | Dhruv Goel     |
|     1 |           15 | 375000.00 |                  2 | 2015-05-01    |         6 | Mukul Gupta    |
|     1 |           15 | 375000.00 |                  2 | 2015-05-01    |         7 | Nancy Aggarwal |
|     1 |           15 | 375000.00 |                  2 | 2015-05-01    |         9 | Prateek Jain   |
|     1 |           15 | 375000.00 |                  2 | 2015-05-01    |        11 | Rajender Gupta |
|     1 |           15 | 375000.00 |                  2 | 2015-05-01    |        12 | S K Goel       |
|     1 |           15 | 375000.00 |                  2 | 2015-05-01    |        13 | Sadhna Goel    |
|     1 |           15 | 375000.00 |                  2 | 2015-05-01    |        15 | Sandeep Jain   |
|     1 |           15 | 375000.00 |                  2 | 2015-05-01    |        16 | Sunil Jain     |
|     1 |           15 | 375000.00 |                  2 | 2015-05-01    |        17 | Sunil Sharma   |
|     1 |           15 | 375000.00 |                  2 | 2015-05-01    |        19 | Sunita Gupta   |
+-------+--------------+-----------+--------------------+---------------+-----------+----------------+
15 rows in set (0.00 sec)

mysql> select date , member_no from bid where gp_no = 1 order by member_no asc;
+------------+-----------+
| date       | member_no |
+------------+-----------+
| 2015-06-01 |         7 |
| 2015-05-01 |        16 |
+------------+-----------+
2 rows in set (0.00 sec)

我想要的:

  1. 我想比较 table 的 member_no 和我得到匹配的地方 它应该显示来自 table bid 的日期,否则它应该显示 不提款。使用我正在使用的代码,我只能显示第一个日期,即只有第一个匹配项。我可能知道这是因为号码不匹配。 tables.
  2. 的行数

但是member_no = 16前面还要显示日期。

PHP代码

$sql = "select * from display where gp_no = '$gp_no' ";
$result = mysqli_query($conn, $sql);

$s_no = 1 ; 
if (mysqli_num_rows($result) > 0)
{


 $s = " select date,member_no  from bid where gp_no = '$gp_no ' order by member_no asc"; 
 $r = mysqli_query($conn, $s);
 if (mysqli_num_rows($r) > 0)
  {
    ?> <h1>Welcome !!! Details of GROUP NO : <?php echo $gp_no ;  ?></h1> 
     <table  align = center ; style="width:50%">
     <tr><th><?php echo " Group Number ";?> </th><td><?php echo $gp_no ; ?></td></tr>
     <tr><th><?php echo " Number of Members ";?> </th><td><?php echo $no_of_member ; ?></td></tr>
     <tr><th><?php echo " Amount ";?> </th><td><?php echo $amount ; ?></td></tr>
     <tr><th><?php echo " Starting Date ";?> </th><td><?php echo $starting_date ; ?></td></tr>
     <tr><th><?php echo " Current Instalment ";?> </th><td><?php echo $current_instalment ; ?></td></tr>
     </table>
     <table  align = center ; style="width:50%" id = " display">
    <tr><th><?php echo "  Sr. Number ";  ?> </th><th><?php echo "  Member Number ";  ?> </th><th><?php echo "  Member Name ";  ?> </th><th><?php echo "  Bid Withdraw ";  ?> </th></tr>
   <?php
   /*$row_count = mysqli_num_rows($result);
   $row1_count = mysqli_num_rows($r);
   $remaining_rows = max($row_count, $row1_count);
   while($remaining_rows-- > 0)
{
    $row = mysqli_fetch_assoc($result);
    $row1 = mysqli_fetch_assoc($r); */
     while($row1 = mysqli_fetch_assoc($r))
     {
         while($row = mysqli_fetch_assoc($result))
    { 


      ?>
     <tr>
        <td><?php echo $s_no ; ?></td>

        <td><?php echo $row["member_no"] ; ?></td>

        <td><?php echo $row["member_name"] ; ?></td>
    <?php
     if($row["member_no"] === $row1["member_no"])
    {
    ?>
       <td><?php echo $row1["date"] ; ?></td>
    <?php 
     }
    else
     { ?> 
       <td><?php echo " Not withdrawn "; ?></td> 
       <?php } ?> 
   </tr> 
  <?php
       $s_no = $s_no + 1;
        }
        } 
        ?>
  </table>  
      <?php 
        }
            } 
    else
    {
    ?> <h1>  Members not associated yet. </h1> <?php
    }
   }
  mysqli_close($conn);
 ?>

为什么不使用 OUTER JOIN,像这样

SELECT d.member_no, b.date
FROM display d LEFT JOIN bid b ON d.member_no = b.member_no
WHERE d.gp_no = 1 AND b.gp_no = 1  

试试这个查询,它应该可以满足您的要求

SELECT d.member_no, d.member_name, IFNULL(b.date, 'NOT WITHDRAWN')
FROM display d LEFT JOIN bid b ON d.member_no = b.member_no
and b.gp_no='1' where d.gp_no='1'

试试这个查询

SELECT d.member_no,d.member_name,CASE WHEN b.date IS NULL THEN 'Not Withdraw' ELSE b.date END AS bidDate FROM display AS d LEFT JOIN bid AS b ON d.member_no=b.member_no WHERE d.gp_no=1

试试这个

SELECT d.member_no,d.member_name,CASE WHEN b.date IS NULL THEN 'Not Withdraw' ELSE b.date END AS bidDate 
FROM display AS d 
LEFT JOIN bid AS b ON d.member_no=b.member_no AND d.starting_date = b.date
WHERE d.gp_no=1