简体   繁体   中英

fetching data from 2 tables having 1 common column

I want fetch records from two efferent tables having user_id in common . but i am able to fetch . given below are the situation and expected results .

table->users

id        name         email           phone        city            users_id
1         abc        abc@xyz.om      9899989989      pqr              BMP-1
2         ijk        ijk@xyz.om      9899989988      jkl              BMP-2



table->requirement

id        label_name      label_value    users_id       requirement_id    date 
103         budget         5000           BMP-1            4            11.03.16
104       specialist      dentist         BMP-1            4            11.03.16
105       treatment       clinic          BMP-1            4            11.03.16
106       expertise       criminal        BMP-2            5            10.03.16
107       charges          5100           BMP-2            5            10.03.16          

expected Result :- It should be in while { }

---------------------------------------------------------

Name:-abc(BMP-1)                          dated:11.03.16

Looking For:- budges       - 5000
              specialist   - dentist
              treatment    - clinick

--------------------------------------------------------------

---------------------------------------------------------
Name:-ijk(BMP-2)                          dated:10.03.16

Looking For:- expertise    - criminal
              charges      - 5100


--------------------------------------------------------------

I have tried so far:-

 <div class="table-responsive"> <table class="table table-hover"> <tbody> <?php $sql=mysql_query( "SELECT * FROM users AS u RIGHT JOIN requirement AS r ON u.users_id=r.users_id"); while($data=mysql_fetch_assoc($sql)){ ?> <!--loop start--> <tr> <td> <div class="row" style="background: #00ACFF; "> <p style="padding: 10px;margin: 0;color: white;"> <i class="fa fa-user"></i> <strong> <?=$data['name']?> </strong> <span style="font-size:85%;"><?=$data['users_id']?></span> <span class="pull-right"> <i class="fa fa-clock-o"></i> <?=$data['date']?> </span> </p> </div> <div class="row" style="background: #EDFEAF; min-height:80px;"> *Looking For<br> <br> -> <?=$data['label_name']?>: <?=$data['label_value']?> <br> </div> <div class="row" style="background: #00ACFF; "> <p style="padding: 10px;margin: 0;color: white;"> <i class="fa fa-envelope"></i> <?=$data['email']?> <i class="fa fa-mobile" style="margin-left:20px"></i> <strong> <?=$data['phone']?> </strong> <span class="pull-right"> <a class="btn btn-default btn-xs" href="#"> View @&nbsp;&nbsp;&nbsp; <i class="fa fa-circle-thin"></i> 12pt </a> </span> </p> </div> </td> </tr> <?php } ?> <!--loop close--> </tbody> </table> </div> 

Please suggest me what should i have to do for this.

Try this solution. It is a little bit long, but it will solve your problem.

    $query = mysql_query("SELECT u.`users_id`, u.`name`, u.`email`, u.`phone`, re.`label_name`, re.`label_value`, re.`requirement_id`, re.`date` FROM users2 u INNER JOIN requirement re ON u.`users_id`=re.`users_id` WHERE 1");
$resultArr = array(); //to store query result
while($row=mysql_fetch_assoc($query))
{
    $resultArr[] = $row;
}
//print_r($resultArr);
?>
<table class="table table-hover">
<tbody>
<?php
$tempUserID = "";
$tempEmail = "";
$tempPhone = "";
$tempReqID = 0; 
for($i=0;$i<count($resultArr);$i++)
{
    //if user id is blank, assign temporary values we need only for one time.
    if($tempUserID=="")
    {
        $tempUserID = $resultArr[$i]['users_id'];
        $tempEmail = $resultArr[$i]['email'];
        $tempPhone = $resultArr[$i]['phone'];
         $tempReqID = $resultArr[$i]['requirement_id'];
        //printing first row
        ?>
        <tr>
        <td>
            <div class="row" style="background: #00ACFF; ">
                <p style="padding: 10px;margin: 0;color: white;">
                    <i class="fa fa-user"></i>
                    <strong> <?=$resultArr[$i]['name']?> </strong>
                    <span style="font-size:85%;"><?=$resultArr[$i]['users_id']?></span>
                    <span class="pull-right">
                        <i class="fa fa-clock-o"></i> <?=$resultArr[$i]['date']?>
                    </span>
                </p>
            </div>
            <div class="row" style="background: #EDFEAF; min-height:80px;">
                *Looking For<br>->
        <?php
    }
    //ok
    if($tempUserID == $resultArr[$i]['users_id'] &&  $tempReqID==$resultArr[$i]['requirement_id'])
    {
        //printing label_name and label_value if users_id is equal to the tempuserid
        ?>

                <br>
                 <?=$resultArr[$i]['label_name']?>: <?=$resultArr[$i]['label_value']?>
                <br>

        <?php
    }
    else
    {
        //if users_id is not equal to the previous one, we will close the first row(i.e.<tr>) and start a new one
        ?>
        </div>
        <div class="row" style="background: #00ACFF; ">
                <p style="padding: 10px;margin: 0;color: white;">

                    <i class="fa fa-envelope"></i> <?=$tempEmail?>
                    <i class="fa fa-mobile" style="margin-left:20px"></i>
                    <strong> <?=$tempPhone?> </strong>

                  <span class="pull-right">
                  <a class="btn btn-default btn-xs" href="#">
                      View @&nbsp;&nbsp;&nbsp;
                      <i class="fa fa-circle-thin"></i> 12pt
                  </a>
                  </span>
                </p>
            </div>
        </td>
    </tr>
        <?php
        //since the users_id is not equal to the previous row, which means that data about new user is available, we will assign new values to temporary variables and start a new table row.
        $tempUserID = $resultArr[$i]['users_id'];
        $tempEmail = $resultArr[$i]['email'];
        $tempPhone = $resultArr[$i]['phone'];
        $tempReqID = $resultArr[$i]['requirement_id'];
        ?>
        <tr>
        <td>
            <div class="row" style="background: #00ACFF; ">
                <p style="padding: 10px;margin: 0;color: white;">
                    <i class="fa fa-user"></i>
                    <strong> <?=$resultArr[$i]['name']?> </strong>
                    <span style="font-size:85%;"><?=$resultArr[$i]['users_id']?></span>
                    <span class="pull-right">
                        <i class="fa fa-clock-o"></i> <?=$resultArr[$i]['date']?>
                    </span>
                </p>
            </div>
           <!--the edited part -->
             <div class="row" style="background: #EDFEAF; min-height:80px;">
                *Looking For<br>->
                 <br>
                 <?=$resultArr[$i]['label_name']?>: <?=$resultArr[$i]['label_value']?>
                <br>
        <?php
    }
}
?>
<!--we will close the table row if current row is the last one in the result-->
<div class="row" style="background: #00ACFF; ">
                <p style="padding: 10px;margin: 0;color: white;">

                    <i class="fa fa-envelope"></i> <?=$tempEmail?>
                    <i class="fa fa-mobile" style="margin-left:20px"></i>
                    <strong> <?=$tempPhone?> </strong>

                  <span class="pull-right">
                  <a class="btn btn-default btn-xs" href="#">
                      View @&nbsp;&nbsp;&nbsp;
                      <i class="fa fa-circle-thin"></i> 12pt
                  </a>
                  </span>
                </p>
            </div>
        </td>
    </tr>
</tbody>
</table>

First query goes like this

 SELECT u.user_id as 'uid',r.date as 'date' 
 FROM users AS u 
 INNER JOIN  requirement r ON u.users_id=r.users_id 
 GROUP BY u.user_id

Join is required to know user registered in requirement table.

Output: Name, Date 2 rows as in example

Then Inside while

 SELECT label_name,label_value FROM requirement 
 WHERE user_id = '$data[uid]'

Output: label_name, label_value for 1st user - 3 rows and 2nd user - 2 rows

You can use and follow this demo: EXAMPLE

SELECT neededfield, CONCAT(firstname, ' ', lastname) as firstlast 
FROM users
WHERE CONCAT(firstname, ' ', lastname) = "Bob Michael Jones"

You should to use CONCATE() Function of mysql.

Name:-abc(BMP-1)

 select u.*,r.* from users u LEFT JOIN requirement r ON u.users_id = r.users_id where r.date = '10.03.16'

 select u.*,r.* from users u LEFT JOIN requirement r ON u.users_id = r.users_id where r.date = '11.03.16'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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