我有一个表,我需要从中获取联系人。 因此,我使用一个复杂的查询从我的“短信”表中检索所有联系人。复杂SQL查询JOIN和其中
但是,它应该只计算提供的“imei”字段的记录,但我的查询会一直返回特定联系人的所有计数。
下面是代码是用的,有什么建议吗?
$sql = $this->db->query('
SELECT
t1.mnumber AS t1_number, t1.mcontent AS t1_content, t1.mcontact as t1_contact, SUM(t2.TOTAL) AS mTotal
FROM
sms t1
JOIN
(SELECT
mContent, mcontact, COUNT(mNumber) AS TOTAL, mnumber, MAX(mID) mID
FROM
sms
GROUP BY mContact) t2 ON t1.mcontact = t2.mcontact
AND t1.mid = t2.mid
WHERE t1.mIMEI = ' . (IMEI) . '
GROUP BY t1.mContact
ORDER BY t1.mid DESC');
return $sql->result();
谢谢你!!
当没有指定组中的“SELECT”列表中的所有列时,请注意在MySQL中使用“GROUP BY”。你的结果可能是不确定的...... – 2013-05-09 15:24:41
你的'WHERE'子句在内部子查询执行其计数之后被评估**。 – 2013-05-09 15:36:05