Php 从具有1个公共列的2个表中提取数据
我想从两个具有共同Php 从具有1个公共列的2个表中提取数据,php,mysql,Php,Mysql,我想从两个具有共同user\u id的传出表中获取记录。但我能去拿。以下是情况和预期结果 table->users id name email phone city users_id 1 abc abc@xyz.om 9899989989 pqr BMP-1 2 ijk ijk@xyz.om
user\u 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
预期结果:-它应该在中,而{}
---------------------------------------------------------
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
--------------------------------------------------------------
到目前为止,我已经尝试过:-
*寻找
-> :
第一个查询是这样的
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需要知道在需求表中注册的用户
输出:Name,Date
2行,如示例所示
然后在里面
SELECT label_name,label_value FROM requirement
WHERE user_id = '$data[uid]'
输出:
label\u name,label\u value
对于第一个用户-3行和第二个用户-2行您可以使用并遵循此演示:
您应该使用mysql的
CONCATE()
函数。试试这个解决方案。虽然有点长,但它会解决你的问题
$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 @
<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 @
<i class="fa fa-circle-thin"></i> 12pt
</a>
</span>
</p>
</div>
</td>
</tr>
</tbody>
</table>
$query=mysql\u query(“选择u.`users\u id`,u.`name`,u.`email`,u.`phone`,re.`label\u name`,re.`label\u value`,re.`requirement\u id`,re.`date`FROM users2 u内部连接要求re ON u.`users\u id`=re.`users\u id`WHERE 1”);
$resultArr=array()//存储查询结果的步骤
while($row=mysql\u fetch\u assoc($query))
{
$resultArr[]=$row;
}
//打印(Resultar);
?>
*正在查找
->
:
*正在查找
->
:
名称:-abc(BMP-1)
@krishna radadiya-我认为我的问题与你的答案不同。我可以理解你的问题,并给出来自任何其他表或一个表的Concate字符串的唯一解决方案,你可以使用Concate函数,所有字符串都是来自任何表的Concate。如果用户填写了2个要求(请参见要求id)。如果是那样的话,就好像是。在您的情况下,如果用户只填写了一个需求,那么您是对的,但是如果用户填写了多个需求,那么我希望这样-@Divakarcool,我已经编辑了我的解决方案。它应该根据你的评论工作。在这两种情况下,我们得到相同的结果。我做了更多的编辑。看一看说“谢谢你这么多朋友终于开始工作了”的部分请尝试将以下调试行立即放在
而{
:var\u dump($data);
之后,然后发布结果。(这将有助于更好地理解这里的问题所在)。不要使用不推荐使用的mysql.*
接口。
$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 @
<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 @
<i class="fa fa-circle-thin"></i> 12pt
</a>
</span>
</p>
</div>
</td>
</tr>
</tbody>
</table>
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'