从两个 table 中获取多个计数并将其保存在 PHP Codeigniter 中的一个变量中
Getting multi count from two table and save it in one variable in PHP Codeigniter
我有两个 table 并且我需要获取这些 table 的数量来满足条件?
table 1: "tbl_comment" table 2: "tbl_group_comment"
两者都有一些共同的列,它们是用户的 login_id,
这是查询:
$param="(SELECT COUNT(*) FROM tbl_comment as t2 WHERE login_id=1) as commentCount, (SELECT COUNT(*) FROM tbl_group_comment as t3 WHERE login_id=1) as commentCount";
$table="`tbl_comment` t2 join `tbl_group_comment`";
$save['value']=$this->Common_model->common_join($param,$table);
这个不起作用,但如果我要更改第二个 commentCount
并将其保留为 commentCount1
,它将给我每个 table 的值,我想得到两个计数的总和?
这件事有没有具体的条款?
试试这个:
select sum(comment_count + group_count) from
((SELECT COUNT(*) as comment_count FROM tbl_comment WHERE login_id=1) A,
(SELECT COUNT(*) as group_count FROM tbl_group_comment WHERE login_id=1)B
);
我有两个 table 并且我需要获取这些 table 的数量来满足条件?
table 1: "tbl_comment" table 2: "tbl_group_comment" 两者都有一些共同的列,它们是用户的 login_id,
这是查询:
$param="(SELECT COUNT(*) FROM tbl_comment as t2 WHERE login_id=1) as commentCount, (SELECT COUNT(*) FROM tbl_group_comment as t3 WHERE login_id=1) as commentCount";
$table="`tbl_comment` t2 join `tbl_group_comment`";
$save['value']=$this->Common_model->common_join($param,$table);
这个不起作用,但如果我要更改第二个 commentCount
并将其保留为 commentCount1
,它将给我每个 table 的值,我想得到两个计数的总和?
这件事有没有具体的条款?
试试这个:
select sum(comment_count + group_count) from
((SELECT COUNT(*) as comment_count FROM tbl_comment WHERE login_id=1) A,
(SELECT COUNT(*) as group_count FROM tbl_group_comment WHERE login_id=1)B
);