dqpu4988 2014-12-25 13:11
浏览 33

如何使用特定值加入两个mysql表?

I have two table one is user and another is tran

Table User

    id      | userName      | email         |Privacy
------------+---------------+---------------+---------------
0           | user1         | email@a.com   |0
25          | user2         | email1@a.com  |0
150         | user3         | emai2@a.com   |1

Table tran

Date     |sender|  amount      | receiver |
---------+------+--------------+----------+
1/1/2013 |user1 |  0           | user2    |
1/2/2013 |user1 |  25          | user3    |
1/2/2013 |user3 |  150         | user1    |

How to join two table for user1 so that i can get the following table(I will set the receiver address according to privacy setting if privacy is 1 then i will show email otherwise i will show the username and type will be set according to the sender and receiver address if user1 is sender then type is transfer otherwise type is received .I don't want this two condition in query because i will be able to set this after query by simply checking the value of the row using 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

I would like to explain a little bit that why i need the join operation.I can fetch the transaction info of user 1 by this way

 select * from transaction where sender='user1' or receiver='user1'  

After fetching the info i will be able to prepare my transaction type in the table based on user1 is the sender or not using just php if else condition .But the problem is i want to mask the username according to the privacy setting of user table so i need to check the that the privacy of the corresponding user is one or not .So i want to join the table user and tran this way so that after fetching data i can just check privacy of the user.

Actually i am working with angularjs and running query like this:

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

And output is

<div class="row">
        <div class="col-md-12" ng-show="filteredItems > 0">
            <table class="table table-striped table-bordered">

            <thead>
            <th>Transaction_number&nbsp;<a ng-click="sort_by('tran_id');"><i class="glyphicon glyphicon-sort"></i></a></th>
            <th>Type&nbsp;<a ng-click="sort_by('tran_type');"><i class="glyphicon glyphicon-sort"></i></a></th>
            <th>Address&nbsp;<a ng-click="sort_by('sender');"><i class="glyphicon glyphicon-sort"></i></a></th>
            <th>Amount&nbsp;<a ng-click="sort_by('amount');"><i class="glyphicon glyphicon-sort"></i></a></th>
            <th>Feee&nbsp;<a ng-click="sort_by('fee');"><i class="glyphicon glyphicon-sort"></i></a></th>

            <th>Date&nbsp;<a ng-click="sort_by('date');"><i class="glyphicon glyphicon-sort"></i></a></th>
            <th>Status&nbsp;<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 exists in user table

  • 写回答

5条回答 默认 最新

  • douzhuangna6906 2014-12-25 13:19
    关注

    You can use LEFT JOIN with the additional Privacy setting in the join condition, and then use COALESCE to default the receiver as anonymous:

    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;
    

    In the case where Privacy is 1, the right hand table columns will be NULL, hence the COALESCE to default.

    SqlFiddle here

    评论

报告相同问题?

悬赏问题

  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP