从包含1个公共列

时间:2016-03-11 10:30:04

标签: php mysql

我希望从两个共同user_id的传出表中获取记录。但我能够取。以下是情况和预期结果。

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          

预期结果: - 应该在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


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

到目前为止我已尝试过: -



<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>
&#13;
&#13;
&#13;

请建议我应该为此做些什么。

4 个答案:

答案 0 :(得分:3)

试试这个解决方案。它有点长,但它会解决你的问题。

    $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>

答案 1 :(得分:1)

首先查询是这样的

 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

需要加入以了解在需求表中注册的用户。

输出:Name, Date 2行,例如

然后在里面

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

第一个用户的输出:label_name, label_value - 3行,第2个用户 - 2行

答案 2 :(得分:0)

您可以使用并遵循此演示:EXAMPLE

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

你应该使用CONCATE() mysql的功能。

答案 3 :(得分:-1)

名称:-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'