我有两个表一个是用户,另一个是tran
表用户
id | userName | email |Privacy
------------+---------------+---------------+---------------
0 | user1 | email@a.com |0
25 | user2 | email1@a.com |0
150 | user3 | emai2@a.com |1
表格
Date |sender| amount | receiver |
---------+------+--------------+----------+
1/1/2013 |user1 | 0 | user2 |
1/2/2013 |user1 | 25 | user3 |
1/2/2013 |user3 | 150 | user1 |
如何为user1加入两个表,以便我可以获得下表(如果隐私为1,我将根据隐私设置设置接收者地址,然后我将显示电子邮件,否则我将显示用户名和类型将根据如果user1是发件人,则发送者和收件人地址然后输入类型,否则收到类型。我不想在查询中使用这两个条件,因为我可以在查询后通过简单地使用行检查行的值来设置php if else condition)
Date | amount | type |address
---------+--------------+-----------------+---------------
1/1/2013 | 0 | transfer |user2
1/2/2013 | 25 | transfer |emai2@a.com
1/2/2013 | 150 | received |user1
我想解释一下为什么我需要连接操作。我可以通过这种方式获取用户1的交易信息
select * from transaction where sender='user1' or receiver='user1'
获取信息后,我将能够在表格中准备我的交易类型,基于user1是发件人或者不使用php if else条件。但问题是我想根据隐私设置屏蔽用户名用户表所以我需要检查相应用户的隐私是否是一个。所以我想加入表用户并转发这种方式,以便在获取数据后我可以检查用户的隐私。
实际上我正在使用angularjs并运行这样的查询:
$query="SELECT t.`tran_id`,t.`tran_type`,t.`sender`,t.`fee`,t.`date`, t.amount, COALESCE(u.email, t.receiver) AS receiver
FROM `transaction` t
LEFT JOIN `user` u
ON u.username = t.receiver
AND u.verify_email = 0;
";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$arr = array();
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$arr[] = $row;
}
}
# JSON-encode the response
$json_response = json_encode($arr);
// # Return the response
echo $json_response;
?>
输出
<div class="row">
<div class="col-md-12" ng-show="filteredItems > 0">
<table class="table table-striped table-bordered">
<thead>
<th>Transaction_number <a ng-click="sort_by('tran_id');"><i class="glyphicon glyphicon-sort"></i></a></th>
<th>Type <a ng-click="sort_by('tran_type');"><i class="glyphicon glyphicon-sort"></i></a></th>
<th>Address <a ng-click="sort_by('sender');"><i class="glyphicon glyphicon-sort"></i></a></th>
<th>Amount <a ng-click="sort_by('amount');"><i class="glyphicon glyphicon-sort"></i></a></th>
<th>Feee <a ng-click="sort_by('fee');"><i class="glyphicon glyphicon-sort"></i></a></th>
<th>Date <a ng-click="sort_by('date');"><i class="glyphicon glyphicon-sort"></i></a></th>
<th>Status <a ng-click="sort_by('status');"><i class="glyphicon glyphicon-sort"></i></a></th>
</thead>
<tbody>
<tr ng-repeat="data in filtered = (list | filter:search | orderBy : predicate :reverse) | startFrom:(currentPage-1)*entryLimit | limitTo:entryLimit">
<td>{{data.tran_id}}</td>
<td>{{data.tran_type}}</td>
<td>{{data.sender}}</td>
<td>{{data.amount}}</td>
<td>{{data.fee}}</td>
<td>{{data.date}}</td>
<td>
{{data.verify_email == 1 ? "data.username" : "data.email"}}
</td>
</tr>
</tbody>
</table>
</div>
** verify_email存在于用户表
中答案 0 :(得分:0)
您可以在加入条件中使用LEFT JOIN和其他隐私设置,然后使用COALESCE
将接收器默认为匿名:
SELECT t.`Date`, t.amount, COALESCE(u.email, t.receiver) AS receiver
FROM `tran` t
LEFT JOIN `User` u
ON u.userName = t.receiver
AND u.Privacy = 0;
在Privacy
为1的情况下,右侧表列将为NULL,因此COALESCE
为默认值。
答案 1 :(得分:0)
检查一下。 select tran.date,tran.amount,(user.privancy = 1,user.email else tran.receiver end)作为user的接收者,tran where user.username = tran.receiver;
答案 2 :(得分:0)
select t.date, t.amount, IF(u.privacy == 1,u.email,u.username)
from User u inner join tran t on u.userName = t.receiver
答案 3 :(得分:0)
使用案例陈述.....
SELECT t。Date
,t.amount,当u.privacy = 1然后是u.email时的情况
否则u.username以接收者身份结束
来自tran
t
LEFT JOIN User
你
ON u.userName = t.receiver;
答案 4 :(得分:-1)
使用以下查询
SELECT t.`Date`, t.amount, COALESCE(u.email,t.receiver) AS receiver
FROM `train` t
LEFT JOIN `test` u
ON u.userName = t.receiver
AND u.Privacy = 1