【问题标题】:select query by id in codeigniter在codeigniter中按id选择查询
【发布时间】:2023-03-17 15:17:01
【问题描述】:

我有两张表是这样的。

test_suite

id    project_id    name
 10       76         tt
 9        76         nn
 8        7          ee

test_suite_run

id    test_suite_id  name
29          10       sss
28          10       ttt
27          9        jjj
26          7        gdgg
25          8        tttt
24          1        oooo

这里,test_suite_id 是 test_suite 表的参考 id。现在我想要一个带有 project_id(say project_id=76)where 查询,输出看起来像这样

id   project_id    test_suite_id  name
29      76               10       sss
28      76               10       ttt
27      76               9        jjj

【问题讨论】:

  • 我是新来的,所以一周的查询。我无法决定如何执行此操作。
  • 为了给您一个很好的答案,如果您还没有看过How to Ask,它可能会对我们有所帮助。如果您可以提供minimal reproducible example,它可能也很有用。

标签: php sql codeigniter


【解决方案1】:

您需要一个简单的 JOINWHERE 子句。

SELECT
     tsr.id
    ,ts.project_id
    ,tsr.test_suite_id
    ,tsr.[name]
FROM test_suite_run tsr
JOIN test_suite ts  
    ON tsr.test_suite_id = ts.id
WHERE ts.project_id = 76

【讨论】:

    【解决方案2】:

    使用活动记录

      $this->db->where('test_suite.project_id', $project_id);
        $this->db->select('*');
        $this->db->from('test_suite');
        $this->db->join('test_suite_run' ,'test_suite.project_id=test_suite_run. project_id');
        $query = $this->db->get();
    

    【讨论】:

      【解决方案3】:

      您需要连接两个表。

      $table = $this->db->select("r.*,t.project_id")
      ->join("test_suite t","t.id = r.test_suite_id")
      ->get('test_suite_run r')->result();
      

      【讨论】:

        【解决方案4】:

        $this->db->select('*'); $this->db->from('test_suite'); $this->db->join('test_suite_run', 'test_suite_run.test_suite_id = test_suite.id'); $this->db->where('test_suite.project_id','76'); $query = $this->db->get();

        【讨论】:

          【解决方案5】:

          这个查询会帮助你..

           $this->db->select('*');
            $this->db->from('test_suite');
            $this->db->join('test_suite_run', 'test_suite_run.project_id = test_suite.test_suite_id');
            $this->db->where('test_suite.project_id =', $project_id);
            $query = $this->db->get();
          

          谢谢你..

          【讨论】:

            【解决方案6】:

            在 codeigniter 中,您可以像这样使用查询生成器......

            $this->db->select('test_suite_run.id,test_suite.project_id,test_suite_run.test_suite_id,test_suite_run.name');
            $this->db->from('test_suite');
            $this->db->join('test_suite_run', 'test_suite_run.test_suite_id   = test_suite.id');
            $this->db->where('test_suite.project_id','76');
            $query = $this->db->get();
            

            从这里获取更多参考...https://www.codeigniter.com/userguide3/database/query_builder.html

            【讨论】:

              【解决方案7】:

              试试这个:

              $CI->db->select('tsr.id,ts.project_id,tsr.test_suite_id,tsr.name');
              $CI->db->from('test_suite_run tsr');
              $CI->db->join('test_suite ts', 'ts.id = tsr.test_suite_id', 'left');
              $query = $CI->db->get(); 
              

              【讨论】:

              • 请详细说明您的解决方案。
              猜你喜欢
              • 1970-01-01
              • 2011-09-18
              • 2015-02-02
              • 1970-01-01
              • 2014-08-26
              • 2021-11-22
              • 1970-01-01
              • 2018-05-08
              相关资源
              最近更新 更多