【发布时间】:2015-12-26 12:27:48
【问题描述】:
我有什么:
1.我有两个数据库表display 和bid,如下所示:
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)
我想要什么:
- 我想比较两个表中的
member_no以及我得到匹配的位置 它应该显示来自表bid的日期,否则它应该显示Not Withdraw。使用我正在使用的代码,我只能显示第一个日期,即只有第一个匹配。我可能知道这是因为号码不匹配。两个表的行数。
但它也应该在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);
?>
【问题讨论】:
-
你可以在这里使用连接。
-
@HarshitShrivastava:怎么做?我的意思是我已经尝试了 Mak 建议的 left , inner join ,但它并没有按照我想要的方式工作。你能推荐一个布局吗?
-
尝试我在答案中发布的查询。这应该工作
标签: php mysql comparison row html-table