ThinkPHP之詭異sql查詢

2021-06-16 14:26:38 字數 4168 閱讀 9899

很久沒用tp了,覺得tp 用久了,都不知道php為何物。來到了公司又用起了tp2.1,行,沒問題,哥之前用過3.1.

問題來了,做的是日考勤和月考勤統計,我汗,tp的關聯查詢,統計查詢,是搞不定了,能搞的話,效率也是不過關,大哥說的,我還體會不到。

參考大哥的sql改吧:

alter table `tbl_attendance` add column `check_unnormal` int null default 1 after `geo_normal`;

alter table `tbl_attendance` drop column `real_name`;

select usr_cal.user_id,usr_cal.real_name,

ifnull(sum(checkintbl.nolate),0) as checkin_late_sum,

ifnull (sum(checkintbl.geo_normal),0) as checkin_geo_unnormal_sum,

ifnull (sum(checkouttbl.nolate),0) as checkout_late_sum,

ifnull (sum(checkouttbl.geo_normal),0) as checkout_geo_unnormal_sum,

ifnull (sum(checkintbl.check_unnormal),0) as checkin_check_sum,

ifnull (sum(checkouttbl.check_unnormal),0) as checkout_check_sum,

ifnull (sum(coalesce(checkintbl.nocheck,1)),0) as checkin_nocheck_sum,

ifnull (sum(coalesce(checkouttbl.nocheck,1)),0) as checkout_nocheck_sum

from

(select usr.user_id,usr.real_name,workday.date from

(select tbl_calendar.* from tbl_calendar

where date >='2013-07-01' and date<='2013-07-06'

and isorwork=0 ) workday,

(select tbl_user.* from tbl_user,tbl_group_member

where tbl_user.user_id = tbl_group_member.user_id

and tbl_group_member.group_id = 1

) usr

) usr_cal

left join

(select user_id,checkdate,nolate,geo_normal,0 as nocheck,check_unnormal from tbl_attendance ,tbl_calendar

where type = 1

and checkdate >='2013-07-01' and checkdate <='2013-07-06'

and tbl_attendance.checkdate = tbl_calendar.date

and tbl_calendar.isorwork=0

) checkintbl

on usr_cal.user_id = checkintbl.user_id and usr_cal.date =checkintbl.checkdate

left join

(select user_id,checkdate,nolate,geo_normal,0 as nocheck,check_unnormal from tbl_attendance,tbl_calendar

where type = 2

and checkdate >='2013-07-01' and checkdate <='2013-07-06'

and tbl_attendance.checkdate = tbl_calendar.date

and tbl_calendar.isorwork=0

) checkouttbl

on usr_cal.user_id = checkouttbl.user_id and usr_cal.date =checkouttbl.checkdate

group by usr_cal.user_id

我去,看的我暈,暈……各種sql知識回顧,各種查詢,總算搞明白了,於是改照出了自己的日考勤sql方案,小有成就感,在資料庫上一測,可以,

$sql = "select usr_cal.user_id,usr_cal.real_name,coalesce(checktbl.nocheck,1)  as nocheck,coalesce(checktbl.nolate,0)  as late,

coalesce(checktbl.geo_normal,0) as geo_normal,checktbl.checktime,checktbl.address

from(

select usr.user_id,usr.real_name,workday.date from

(select tbl_calendar.* from tbl_calendar

where date="."'".$date."'"."

and isorwork=0 ) workday,

(select tbl_user.* from tbl_user,tbl_group_member

where tbl_user.user_id = tbl_group_member.user_id

and tbl_group_member.group_id = $group_id and tbl_user.type!=-11 and tbl_group_member.user_type = 4

) usr

) usr_cal

left join

(select user_id,checkdate,nolate,checktime,geo_normal,0 as nocheck,check_unnormal,address from (select tbl_attendance.*,tbl_geo.address from tbl_attendance,tbl_geo

where tbl_attendance.geo_id = tbl_geo.geo_id and tbl_attendance.group_id = $group_id

) attend ,tbl_calendar

where type = $type

and checkdate ="."'".$date."'"."

and attend.checkdate = tbl_calendar.date

and tbl_calendar.isorwork=0

) checktbl

on usr_cal.user_id = checktbl.user_id and usr_cal.date =checktbl.checkdate where $where_search

group by usr_cal.user_id order by nocheck desc,late asc,geo_normal asc

";

好了,交給tp了:。

不管是2.1還是3.1都是這麼說的,我怎麼嘗試怎麼不行,怎麼查怎麼不行,$model = m();網上的解決方案,也不行,問梁哥,說隨便例項化乙個物件就行了,各種嘗試

$tag = m('tag');

$tag = new model('tag');

$tag->query('select * from tbl_tag');這麼簡單的都不行,我徹底無語,崩潰了,

哥過來了說都跟你說了隨便例項化乙個物件,一邊開啟groupaction,一邊說都不知道怎麼說你

貼上了$group = d('group');$group->query($sql); 結果出來了,就這麼出來了,怎麼就這麼不靈活呢,就不知道m和d換著試試,

怎麼就這麼不靈活呢,自己在一遍一遍的告訴自己

DML之SQL增刪改查

dml 資料操作語言 適用物件 表中的資料 功能 1 向表中新增資料 mysql insert into goods id,name,price,type values 2,褲子 100,服裝 注意 如果我們新增資料時,每一列的值都不為空的話,我們可以省去列,如下 mysql insert into...

SQL 語法之「增」 「刪」 「改」 「查」

四 查1.普通查詢 語法 select 列名 from 表名 where 查詢條件表達試 order by 排序的列 名 asc或desc 1 查詢全部資料行和列 例 select from a 說明 查詢a表中全部行和 2 查詢部分行列 條件查詢 例 select i,j,k from a whe...

thinkphp驗證碼無法顯示的詭異解決

今天專案中的thinkphp驗證碼無法顯示,遂上論壇搜尋,問題最多的還是在bom,有人說關閉debug後就能顯示驗證碼,試了試,果然,關閉debug就正常了,但是一開啟debug就還是無法顯示了.怎麼回事呢?驗證碼要使用驗證碼,需要匯入擴充套件類庫中的org.util.image類庫和org.uti...