我想从7个表

时间:2018-02-15 09:03:25

标签: php mysql codeigniter

我需要帮助查询,我们从7个表中获取数据。我的任务是:我有一个crs列表的下拉列表,所以当我选择一门课程时,我需要在课程,测试和用户的组合中获得数据。

输出如下:

name| test 
--- |-----
sai | test

table object_data:

obj_id | type | title 
---    |------|------
3217   |crs   |it 
3221   |grp   |xyz 
3228   |tst   |test

object_reference

ref_id | obj_id 
---    |---------
337    |3217       
338    |3221      
343    |3228 

tree | child | parent 
---  |-------|------
1    |338    |337 
2    |343    |338 

object_members

obj_id| usr_id 
---  |-------
3217 |3213
3221 |3213

usr_data

usr_id|name
---  |-------
3213|sai

tst_tests

test_id| obj_id
---  |-------
1    |3228

tst_tests

test_id| obj_id
---  |-------
122    |3228

tst_active

test_id| obj_id
---  |-------
122    |3213

上表包含obj_members数据中所有data.crs和用户组合以及tst_teststst_active中的测试和用户组合以及{{1}中的测试和课程组合的组合和树表。

任何人都可以帮我一个查询吗?我尝试了三个查询,但我没有得到结果。以下是我的疑问。

用户和课程组合:

object_reference

课程和考试组合:

$users_query="SELECT o.title as course_title,u.*,u.usr_id,o.obj_id FROM object_data o,usr_data u,obj_members rn WHERE o.obj_id=rn.obj_id AND u.usr_id=rn.usr_id AND o.type='crs'  AND o.obj_id=$course";

用于用户和测试组合:

$user_course_query ="SELECT  *,(select obj_id FROM `object_reference` obr WHERE obr.ref_id = c.child) as final_reference FROM object_data a JOIN object_reference b ON a.obj_id = b.obj_id JOIN tree c ON b.ref_id = c.parent JOIN object_reference d ON c.child = d.ref_id JOIN tree e ON d.ref_id = e.parent JOIN object_reference f ON e.child = f.ref_id JOIN object_data g ON f.obj_id = g.obj_id and a.obj_id=$course "

0 个答案:

没有答案