php - Complex SQL query JOIN and WHERE -
i have table, need contacts from. use complex query retrieve contacts "sms" table.
however, should count records provided "imei" field, query keeps returning counts specific contact.
below code use, advice?
$sql = $this->db->query(' select t1.mnumber t1_number, t1.mcontent t1_content, t1.mcontact t1_contact, sum(t2.total) mtotal sms t1 join (select mcontent, mcontact, count(mnumber) total, mnumber, max(mid) mid sms group mcontact) t2 on t1.mcontact = t2.mcontact , t1.mid = t2.mid t1.mimei = ' . (imei) . ' group t1.mcontact order t1.mid desc'); return $sql->result(); thanks you!!
better formatted, query is:
select t1.mnumber t1_number, t1.mcontent t1_content, t1.mcontact t1_contact, sum(t2.total) mtotal sms t1 join (select mcontent, mcontact, count(mnumber) total, mnumber, max(mid) mid sms group mcontact ) t2 on t1.mcontact = t2.mcontact , t1.mid = t2.mid t1.mimei = ' . (imei) . ' group t1.mcontact order t1.mid desc the problem where clause in wrong place. affect count, need in subquery. affect output rows, need in outer query. suggest:
select t1.mnumber t1_number, t1.mcontent t1_content, t1.mcontact t1_contact, sum(t2.total) mtotal sms t1 join (select mcontent, mcontact, count(mnumber) total, mnumber, max(mid) maxmid sms sms.mimei = ' . (imei) . ' group mcontact ) t2 on t1.mcontact = t2.mcontact , t1.mid = t2.maxmid group t1.mcontact order t1.mid desc i suspect there may other simplifications, depending on mid andmcontact mean in terms of database structure.
Comments
Post a Comment