在codeigniter中按id选择查询

时间:2017-01-03 11:29:13

标签: php sql codeigniter

我有两张桌子。

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(比如project_id = 76)进行 where 查询,其中输出看起来像

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

7 个答案:

答案 0 :(得分:2)

您想要一个带JOIN子句的简单WHERE

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

答案 1 :(得分:0)

使用活动记录

  $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();

答案 2 :(得分:0)

此查询将帮助您..

 $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();

谢谢..

答案 3 :(得分:0)

你需要连接两个表。

$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 :(得分:0)

试试这个:

$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(); 

答案 5 :(得分:0)

$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();

答案 6 :(得分:0)

在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