"表1"
$this->db->select('referral1.*, client.*, employee.* ');
$this->db->from('client');
$this->db->join('referral1', 'client.referral_id = referral1.referral1_id', 'inner' );
$this->db->join('assign_psychotherapist ', 'assign_psychotherapist.a_referral_id = client.referral_id', 'inner' );
$this->db->join('employee ', 'assign_psychotherapist.a_psychotherapist_id = employee.empid', 'inner' );
$this->db->where("referral_status ='Assigned' OR referral_status ='Accepted' ");
$this->db->order_by("referral_date", "desc");
"表2"
$this->db->select('referral1.*, client.*, volunteer.* ');
$this->db->from('client');
$this->db->join('referral1', 'client.referral_id = referral1.referral1_id', 'inner' );
$this->db->join('assignvolunteer', 'assignvolunteer.Vreferralid = client.referral_id', 'inner' );
$this->db->join('volunteer', 'assignvolunteer.Vvolunteerid = volunteer.volid', 'inner' );
$this->db->where("referral_status ='Assigned' OR referral_status ='Accepted' ");
$this->db->order_by("referral_date", "desc");
如何将两个查询一起加入?
答案 0 :(得分:0)
你可以这样做:
$sql = ("
SELECT *
FROM (
select r.referral_date,c.lastname,c.middlename,c.firstname,c.gender,r.presenting_problem,e.employee_nickname
from client c
inner join referral1 r
on client.referral_id = referral.referral1_id
inner join assign_psychotherapist ap
on ap.a_referral_id = c.referral_id
inner join employee e
on ap.a_psychotherapist_id = e.empid
where referral_status ='Assigned' OR referral_status ='Accepted'
order by referral_date desc
)
union all
SELECT * FROM
(
select r.referral_date,c.lastname,c.middlename,c.firstname,c.gender,r.presenting_problem,v.volunteer_nickname
from client c
inner join referral1 r
on c.referral_id = r.referral1_id
inner join assignvolunteer av
on av.Vreferralid = c.referral_id
inner join volunteer v
on v.Vvolunteerid = v.volid
where referral_status ='Assigned' OR referral_status ='Accepted'
order by referral_date desc
)
");
$this->db->query($sql);
由于Code Igniter自版本2.2.1起不支持活动记录中的UNION, 您可以使用查询字符串来实现所需的结果。
附注:使用UNION ALL
时,请确保要加入的查询包含相同数量和相同的列位置,以实现您想要的效果。
您还可以使用表别名来进一步缩短查询并使其更有条理。
答案 1 :(得分:0)
可以在这里找到更好的答案 - write union query in codeigniter style简而言之使用get_compiled_select()并创建自己的查询。
答案 2 :(得分:0)
SELECT * FROM
(
SELECT DISTINCT assessment_topic.id AS NIRUPAM, assessment_topic.name, assessment_update.* FROM
assessment_topic
LEFT JOIN assessment_update ON assessment_topic.id = assessment_update.assess_id
UNION
SELECT assessment_topic.id, assessment_topic.name, '', 9, '', '', ''
FROM assessment_topic
) A
WHERE A.school_id = 9
GROUP BY A.name
LIMIT 8
// 9动态id传递和8动态限制传递