简体   繁体   English

如何从mysql提取行到表头以及与该头相关的特定数据

[英]how to fetch rows from mysql to table header and specific data related with that fetched header

i have 2 tables question_details and paid_response 我有2个表question_detailspaid_response

question_details contains question_details包含

 qno  qshortcode
 504   what do you want
 515   what is your name
 541   what is your address
  .
  .
 other..  others question

paid_response contains paid_response包含

 qno   paid_respo   paid_rev    sys_date
 504    yes         0.60       2014-12-16 04:14:40
 515    no          0.42       2014-12-17 04:14:40 

now i want qshortcode from question_details with given qno(504 and 515) and paid_respo from paid_response table where paid_rev not=0.00 and between two dates 现在我想qshortcodequestion_details具有给定qno(504 and 515)和从paid_respo paid_response表,其中不paid_rev = 0.00和两个日期之间

what do you want    what is your name   //fetching from `question_details` table
yes                  no                 //fetching from `paid_response` table with respect to `qno` where paid_rev not 0.00

my code for fetching qshortcode from question_details` table 我的代码qshortcode from question_details`表中获取qshortcode from

<?php
//DB connection goes here

$query=mysql_query("select qshortcode from question_details where qno=504 or qno='515'");   
echo '<tr>';
for($i = 0; $row = mysql_fetch_array($query);$i++) {
echo '<td>'.$row['qshortcode'].'</td>';           
echo '</tr>';                        
} ?>     

its fetching like 它像

what do you want what is your name

 //cant fetch paid_respo of specific qshortcode below this where paid_rev not euqals to 0.00 or blank

try using this:use join to combine both the tables 尝试使用此方法:使用联接 合并两个表

<?php
//DB connection goes here

$query=mysql_query("select qshortcode,paid_respo from question_details left join paid_response on paid_response.qno=question_details.qno  where question_details.qno in (504,515)");   


     echo '<table>';
        while ($row = mysql_fetch_array($query)) {
        echo '<tr>';//to show each response as one row.
        echo '<td>'.$row['qshortcode'].'</td>';//what do you want
        echo '<td>'.$row['paid_respo'].'</td>';//yes            
        echo '</tr>';                        
        } 
    echo '</table>'

?>  

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

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