连接表多次显示相同的条目

时间:2014-08-21 11:51:06

标签: php mysql wordpress

我的db表结构:

table1: - wp_eemail_newsletter_sub

    id| eemail_name_sub |eemail_email_sub |eemail_mobile_sub |patient_date
    ------------------------------------------------------------------------
    1 | user1    |  user1@gmail.com | 9999999999 | 2014-5-14
    ---------------------------------------------------------
    2 | user2 | user2@gmail.com | 754712456 |2014-8-11

table2: - wp_on​​line_counsultation_query

id| online_consult_file | online_consult_filename | online_consult_datetime | consult_patientid
 ======================================================================================================
 1 | filepath | filename1 | 2014-8-12 | 999999999
 -------------------------------------------------
 2 | filepath | filename2 | 2014-8-14 | 754712456
 -------------------------------------------------
 3 | filepath | filename3 | 2014-8-14 | 999999999
 -------------------------------------------------
 4 | filepath | filename4 | 2014-8-22 | 999999999

db中发生的事情是,当患者注册时,他的详细信息将进入表1,并且患者的手机号码被用作识别每个患者的唯一ID。因此,在注册后,管理员可以从管理员部分上传患者的文件报告,并且文件和文件路径与患者移动号码一起存储在表2中。所以一名患者有多个文件报告。现在我需要将它们显示为网站页面中的表格,因为我使用的格式为:

SerialNo: | Name | Email | Mobile | Test Date | Reports
========================================================

 1 | user1 | user1@gmail.com | 9999999999 | 2014-5-14 | filename1 , filename3, filename4
----------------------------------------------------------------------------------------
 2 | user2 | user2@ymail.com | 754712456 | 2014-8-11 | filename2

我尝试了连接查询:

SELECT DISTINCT wp_eemail_newsletter_sub.eemail_name_sub,wp_eemail_newsletter_sub.eemail_email_sub,wp_eemail_newsletter_sub.eemail_mobile_sub,wp_eemail_newsletter_sub.patient_date,wp_online_consultation_querys.online_consult_file,wp_online_consultation_querys.online_consult_filename FROM wp_eemail_newsletter_sub LEFT JOIN wp_online_consultation_querys ON wp_eemail_newsletter_sub.eemail_mobile_sub=wp_online_consultation_querys.consult_patientid ORDER BY wp_online_consultation_querys.online_consult_datetime desc

这使我得到了这张表:

user1 | user1@gmail.com | 999999999 | 2014-5-14 | filename1
user1 | user1@gmail.com | 999999999 | 2014-5-14 | filename3
user1 | user1@gmail.com | 999999999 | 2014-5-14 | filename4
user2 | user2@gmail.com | 754712456 | 2014-8-11 | filename1

任何人都可以告诉我如何在php中以上述所需的表格格式显示查询结果数据。

编辑: - 使用jimmy的答案 用于在站点中显示结果的PHP代码是:

$test = 'SELECT  DISTINCT  wp_eemail_newsletter_sub.eemail_name_sub, wp_eemail_newsletter_sub.eemail_email_sub, wp_eemail_newsletter_sub.eemail_mobile_sub, wp_eemail_newsletter_sub.patient_date,wp_online_consultation_querys.online_consult_file,GROUP_CONCAT( wp_online_consultation_querys.online_consult_filename) As s FROM wp_eemail_newsletter_sub LEFT JOIN wp_online_consultation_querys ON wp_eemail_newsletter_sub.eemail_mobile_sub= wp_online_consultation_querys.consult_patientid GROUP BY wp_eemail_newsletter_sub.eemail_mobile_sub ORDER BY wp_online_consultation_querys.online_consult_datetime desc';
$testdatea = $wpdb->get_results($test);

<table width="100%" class="widefatt" id="straymanage">
<thead>
  <tr>
    <th width="2%" align="left"></th>
    <th width="3%" align="left">Sno</th>
    <th width="5%" align="left">ID</th>
    <th width="8%" align="left">Name</th>
    <th width="8%" align="left">Email</th>
     <th width="5%" align="left">Mobile</th>
     <th width="10%" align="left">Test</th>
     <th width="7%" align="left">Test Date</th>
     <th width="7%" align="left">Latest Reports</th>
    <th width="6%" align="left">Action</th>
    </tr> 
   <thead>
    <tbody>
  <?php
   if ( ! empty($testdatea) ) 
   {
     ?>
     <?php 
     $i = 1;
     foreach ( $testdatea as $data ) {

      ?>
  <tr class="<?php if ($i&1) { echo'alternate'; } else { echo ''; }?>">
     <td align="left"><input class="case" name="chk_delete[]" id="chk_delete[]" type="checkbox" value="<?php echo(stripslashes($data->eemail_id_sub)).'_'.(stripslashes($data->eemail_email_sub)).'_'.(stripslashes($data->eemail_mobile_sub)); ?>" />   </td>  
    <td align="left"><?php if(($_GET['paginate']) != '') { echo ($limit * ($_GET['paginate']-1))+ $i; } else echo $i; ?></td>  
    <td align="left"><?php echo($data->eemail_patient_id); ?></td>
             <td align="left"><?php echo(stripslashes($data->eemail_name_sub)); ?></td> 
    <td align="left"><?php echo(stripslashes($data->eemail_email_sub)); ?></td> 
    <td align="left"><?php echo(stripslashes($data->eemail_mobile_sub)); ?></td> 
     <td align="left"><?php echo(stripslashes($data->diagnosis_name)); ?></td> 
     <td align="left"><?php echo(date("d M ,Y", strtotime($data->patient_date)));  ?></td>
     <td align="left"><a href="javascript:void(0);" onclick="showdiv('<?php echo $data->online_consult_file;?>')"><?php echo(stripslashes($data->s));?></td>


     <td align="left">
         <a title="Delete" onClick="javascript:_subscriberdealdeletebylabs('<?php echo($data->eemail_patient_id); ?>')" href="javascript:void(0);">Delete</a> </td> 

      </tr>
       <?php $i = $i+1;  } ?>
      </tbody>
       <?php
       }

     else
       {
      ?>
 <tr><td colspan="5">No data found</td></tr>
       <?php
        } ?>
    </table>

Query为我提供了正确格式的表格,其中包含一个用户的“filename1,filename3,filename4”文件。但我需要他们像

<a href="filepath">filename1</a>,<a href="filepath">filename3</a>

请帮我解决这个问题?

2 个答案:

答案 0 :(得分:0)

你需要做GR​​OUP BY eemail_mobile_sub

和GROUP_CONCAT(online_consult_filename)

答案 1 :(得分:0)

对于更好的编码做法,我建议应该遵循这个: -

1)在SELECT

中包含GROUP_CONCAT(online_consult_file)AS FILEPATH

2)现在你将获得FILEPATH中的文件路径,文件路径,文件路径等数据,并且你在FILENAME中有filename1,filename3,filename4

3)现在你可以在用逗号分隔的php中爆炸,这样你就可以得到两个数组$ FILEPATH和$ FILENAME

4)现在你可以使用for循环: -

$noOfFiles  = count($FILENAME);
for($i=0;$i<$noOfFiles;$i++) {
   echo '<a href="'.$FILEPATH[$i].'">'.$FILENAME[$i].'</a>' // Here you would get all the links one by one
}
相关问题