【问题标题】:How to create a table view by joining two tables without overlapping records with same relation id [duplicate]如何通过连接两个表而不重叠具有相同关系ID的记录来创建表视图[重复]
【发布时间】:2016-06-23 10:29:00
【问题描述】:

我有一个表来存储所有代理活动,如下所示,

+----+---------+-------------+-------------+
| id | subject |    type     | relation_id |
+----+---------+-------------+-------------+
|  1 | Call    | lead        |          25 |
|  2 | Visit   | Opportunity |          25 |
+----+---------+-------------+-------------+
怎么可能做出这样的看法。
+----+---------+-----------+------------------+
| id | Subject |   lead    |   Opportunity    |
+----+---------+-----------+------------------+
|  1 | Call    | lead_name |                  |
|  2 | Visit   |           | Opportunity_name |
+----+---------+-----------+------------------+

【问题讨论】:

  • 请在这里分享您的努力。到目前为止您尝试了什么?

标签: php mysql codeigniter


【解决方案1】:
$this->db->join('Opportunity','Opportunity.id = store_all_agent.relation_id','left');
$query = $this->db->get('store_all_agent');
return $query->result_array();

参考:https://www.codeigniter.com/userguide2/database/active_record.html

【讨论】:

    【解决方案2】:

    您需要使用 CASE 来获得所需的结果:

    SELECT id,subject,
    CASE WHEN type='lead' THEN 'lead_name' ELSE '' END as lead,
    CASE WHEN type='Opportunity' THEN 'Opportunity_name' ELSE '' END as Opportunity
    FROM mytable
    

    【讨论】:

    • 这与重复主题链接中描述的概念完全相同。
    猜你喜欢
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2012-05-22
    • 2017-07-12
    • 2019-10-27
    • 1970-01-01
    相关资源
    最近更新 更多