>  Q&A  >  본문

mysql - ThinkPHP3.1 多变关联查询

请问我怎么根据日志表的log_id字段取得客户表的name字段呢?用tp的写法,求解

日志表:log
+------------------+--------------+------+-----+---------+----------------+
| log_id           | int(10)      | NO   | PRI | NULL    | auto_increment |
| role_id          | int(11)      | NO   |     | NULL    |                |
| category_id      | int(10)      | NO   |     | NULL    |                |
| create_date      | int(10)      | NO   |     | NULL    |                |
| update_date      | int(10)      | NO   |     | NULL    |                |
| subject          | varchar(200) | NO   |     | NULL    |                |
| content          | text         | NO   |     | NULL    |                |
| comment_id       | int(10)      | NO   |     | NULL    |                |
| about_roles      | varchar(200) | NO   |     | NULL    |                |
| about_roles_name | varchar(500) | NO   |     | NULL    |                |
| belong_to_user   | varchar(30)  | NO   |     | NULL    |                |
| customer         | varchar(155) | YES  |     | NULL    |                |
+------------------+--------------+------+-----+---------+----------------+

日志客户中间表:r_customer_log
+-------------+---------+------+-----+---------+----------------+
| id          | int(10) | NO   | PRI | NULL    | auto_increment |
| customer_id | int(10) | NO   |     | NULL    |                |
| log_id      | int(10) | NO   |     | NULL    |                |
+-------------+---------+------+-----+---------+----------------+

客户表:customer

+----------------------+--------------+------+-----+---------+----------------+
| customer_id          | int(10)      | NO   | PRI | NULL    | auto_increment |
| owner_role_id        | int(10)      | NO   |     | NULL    |                |
| creator_role_id      | int(10)      | NO   |     | NULL    |                |
| contacts_id          | int(10)      | NO   |     | 0       |                |
| name                 | varchar(333) | NO   |     |         |                |
| origin               | varchar(150) | NO   |     |         |                |
| industry             | varchar(150) | NO   |     |         |                |
| annual_revenue       | varchar(20)  | NO   |     | NULL    |                |
| create_time          | int(10)      | NO   |     | NULL    |                |
| update_time          | int(10)      | NO   |     | NULL    |                |
| is_deleted           | int(1)       | NO   |     | NULL    |                |
| is_locked            | int(1)       | NO   |     | NULL    |                |
| delete_role_id       | int(10)      | NO   |     | NULL    |                |
| delete_time          | int(10)      | NO   |     | NULL    |                |
| biz_type             | varchar(255) | NO   |     |         |                |
+-------------+---------+------+-----+---------+------------------------------+
迷茫迷茫2772일 전376

모든 응답(1)나는 대답할 것이다

  • 高洛峰

    高洛峰2017-04-11 08:59:57

    1这个是sql的多表关联
    我先写个sql吧!

    select c.name as name  from r_customer_log as cl  left join customer as c on c.customer_id=cl.customer_id left join log as l on l.log_id=cl.log_id where cl.log_id=1
    $r_customer_log=M('r_customer_log');
    $where['cl.log_id']=1;
    $join1="  left join customer as c on c.customer_id=cl.customer_id ";
    $join2="  left join log as l on l.log_id=cl.log_id ";
    $r=$r_customer_log->alias('cl')->join($join1)->join($join2)->where($where)->select();
    

    就是这样写的

    회신하다
    0
  • 취소회신하다