2017-01-03 93 views
-3

我有两张表看起来像那样。在codeigniter中按ID选择查询

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是参考ID与test_suite表。现在我想其中查询与PROJECT_ID(比如PROJECT_ID = 76)其中输出如下

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

我是新来的,所以一周查询。我无法决定如何执行此操作。 – user7149801

+0

为了给你一个很好的答案,如果你有一个问题,你可以帮助我们,如果你还没有。如果你可以提供[mcve],它可能也很有用。 – Mat

回答

2

你想要一个简单的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 
+0

哦谢谢。它的工作原理 – user7149801

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

谢谢。

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(); 
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(); 
+0

请详细解释您的解决方案。 – Juve

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

0

CI中您可以使用查询生成器,这样....

$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