我正在使用CodeIgniter,我的问题是关于MySQL查询的。 我有两个表,分别是成员和关系。
成员表
我正在做的是,我正在根据member_type
在成员表中添加所有用户。如果member_type为1,则为主要成员;如果为2,则为次要成员
关系表
在关系表中,我正在定义用户的关系。例如primary_member_id
为1,则次要成员为3和6,次要成员详细信息在成员表中可用。
现在,我需要查询以显示列表中的所有主要成员,并与它们的次要成员名称和其他详细信息有关。
我正在使用带有子行的数据表。这样我就可以在列表中显示主要名称,然后单击加号以显示次要成员。
我尝试了以下联接代码。
SELECT * FROM `member` LEFT JOIN relation on member.id=relation.primary_member_id WHERE member.member_type=1
下面的是上面查询的输出。在这种情况下,我要获得两次主要成员名称以及如何显示次要成员名称
我需要一个输出
数据表
function format ( d ) {
return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'+
'<tr>'+
'<td>Full name:</td>'+
'<td>'+d.name+'</td>'+
'</tr>'+
'<tr>'+
'<td>Mobile number:</td>'+
'<td>'+d.phone+'</td>'+
'</tr>'+
'<tr>'
'</table>';
}
$(document).ready(function() {
var oTable =$('#all_list').DataTable( {
"processing": true,
// "serverSide": true,
"pageLength": 10,
"ajax": {
"url": baseUrl+ "/index.php/Search/Listdetails_ajax",
"type": "POST"
},
"columns": [
{
"className": 'details-control',
"orderable": false,
"data": null,
"defaultContent": ''
},
{ "data": "profile_pic","className":"img_list_pic",
"render": function (data, type, full, meta) {
return '<img src='+baseUrl+'/uploads/images/'+data+' class="search_pic">';
}
},
{ "data": "name" },
{ "data": "phone" }
],
"order": [[1, 'asc']],
} );
$('#all_list tbody').on('click', 'td.details-control', function () {
var tr = $(this).closest('tr');
var row = oTable.row( tr );
if ( row.child.isShown() ) {
// This row is already open - close it
row.child.hide();
tr.removeClass('shown');
}
else {
// Open this row
row.child( format(row.data()) ).show();
tr.addClass('shown');
}
} );
} );
ajax
public function Listdetails_ajax(){
$draw = intval($this->input->get("draw"));
$start = intval($this->input->get("start"));
$length = intval($this->input->get("length"));
$books = $this->Search_model->getTotalList_of_primary();
$books_of_secondary = $this->Search_model->getTotalList_of_secondary();
$data['draw'] = 1;
$data['recordsTotal'] = count($books);
$data['recordsFiltered'] = count($books);
foreach ($books as $key => $row)
{
$arr_result = array(
"profile_pic" => $row->profile_pic,
"name" => $row->first_name.' ' .$row->last_name,
"phone" => $row->phone
);
$data['data'][] = $arr_result;
}
//print_r($arr_result);
echo json_encode($data);
exit;
}
/ 模型 /
public function getTotalList_of_primary(){
$this->db->select('*');
$this->db->from('member');
$this->db->where('member_type',1);
$query = $this->db->get();
$res = $query->result();
return $res;
}
public function getTotalList_of_secondary(){
/*what query I have to use here*/
}
答案 0 :(得分:1)
遵循details row中所使用的详细信息弹出框的想法,控制器将如下所示:
public function Listdetails_ajax(){
$draw = intval($this->input->get("draw"));
$start = intval($this->input->get("start"));
$length = intval($this->input->get("length"));
$books = $this->Search_model->getTotalList_of_primary();
$data['draw'] = 1;
$data['recordsTotal'] = count($books);
$data['recordsFiltered'] = count($books);
foreach ($books as $key => $row)
{
//print_r($row->customer_id);
$arr_result = array(
"member_id" => base64_encode($this->encryption->encrypt($row->member_id)),
"profile_pic" => $row->profile_pic,
"name" => $row->first_name.' ' .$row->last_name,
"phone" => $row->phone,
"chss_no" => $row->chss_no,
"emp_id" => $row->emp_id,
"address" => $row->address
// "member_type" => $row->member_type
);
$array_secondary = array();
$books_of_secondary = $this->Search_model->getTotalList_of_secondary($row->customer_id);
foreach ($books_of_secondary as $key => $row)
{
//print_r($row->customer_id);
$arr_result2 = array(
"s_member_id" => base64_encode($this->encryption->encrypt($row->member_id)),
"s_profile_pic" => $row->profile_pic,
"s_name" => $row->first_name.' ' .$row->last_name,
"s_phone" => $row->phone,
"s_chss_no" => $row->chss_no,
"s_emp_id" => $row->emp_id,
"s_address" => $row->address
);
$array_secondary[] = $arr_result2;
}
$arr_result['secondary'] = $array_secondary;
$data['data'][] = $arr_result;
}
echo json_encode($data);
exit;
}
然后在format函数中,您将访问secondary
字段以获取第二数组。也许像这样:
function format(d) {
// d is the original data object for the row var val;
if(d.secondary.length == 0) {
return "There are no secondary members";
}
var display = '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">';
for (val of d.secondary) {
display += '<tr>' + '<td>Full name:</td>' + '<td>' + val.s_name + '</td>' + '</tr>' + '<tr>' + '<td>Mobile number:</td>' + '<td>' + val.s_phone + '</td>' + '</tr>';
}
display += '</table>';
return display;
}
答案 1 :(得分:0)
您可以使用两个查询来实现。
首先获取所有主要成员以显示主要成员列表。
select * from Member m where m.member_type = 1;
第二个获取该主要成员ID的所有次要成员。
select * from Member m
join Relation r where m.member_id = r.secondary_member_id and
r.primary_member_id = :primaryMemberId;