3.MySQL Left Join左连接后对数据做SUM汇总处理时出现数据倍数增长问题(左连接SUM数据重复问题)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
select b.id,b.plate_number,SUM( bx.pay_money),SUM(o.money) from base b left join baoxian bx on b.id=bx.plate_number left join oil o on b.id=o.plate_number GROUP BY b.id
select b.*,bx.bx_money,o.oil_money,jq.jq_money from base b left join (select SUM(pay_money) AS bx_money,plate_number from baoxian GROUP BY plate_number) AS bx on b.id=bx.plate_number left join (select SUM(money) AS oil_money,plate_number from oil GROUP BY plate_number) AS o on b.id=o.plate_number left join (select SUM(money) AS jq_money,plate_number from jiaoqiang GROUP BY plate_number) AS jq on b.id=jq.plate_number
SELECT a.uid,a.user_name,count(DISTINCT b.id) as task_num,count(DISTINCT d.id) as task_pass_num,count(DISTINCT e.id) as test_pass_num,c.template_total_money,count(DISTINCT f.id) as market_template_num FROM hh_xxx_designer_ranking a left join hh_xxx_designer_task_project b on a.uid=b.uid and b.receive_time > 0 LEFT JOIN (select sum(money) as template_total_money,uid,create_time,type from hh_xxx_payment_log group by uid ) as c on a.uid=c.uid and c.create_time > 1635945200000 and c.type = 2 LEFT JOIN hh_xxx_designer_task_project d on a.uid=d.uid and d.complete_time > 0 LEFT JOIN hh_xxx_designer_task_project e ON a.uid=e.uid and e.test_adopt_time > 0 LEFT JOIN hh_xxx_market_template f ON a.uid=f.uid and f.adopt_time > 0 where a.uid = 999 GROUP BY a.id;
LEFT JOIN (select sum(money) as template_total_money,uid,create_time,type from hh_xxx_payment_log group by uid ) as c on a.uid=c.uid and c.create_time > 16359599900000 and c.type = 2 修改为 LEFT JOIN (select sum(money) as template_total_money,uid,create_time,type from hh_xxx_payment_log where create_time > 16359599900000 and type = 2 group by uid ) as c on a.uid=c.uid where a.uid = 967 GROUP BY a.id;
4.leftjoin 一对多 只获得一条数据
解决办法,去掉select中id字段
5.如何 distinct 只对一个字段有用,如何同时查出其他字段
distinct只能返回它的目标字段
select distinct name, id from table
1 2 3 4 5 6 7
id name 1 a 2 b 3 c 4 c 5 b 作用是起了的,不过他同时作用了两个字段
select id, distinct name from table
1
报错,distinct必须放在开头,distinct放到where条件里?能,照样报错
解决办法:
在mysql手册里找到一个用法,用group_concat(distinct name)配合group by name可以实现实现了。
select *,group_concat(distinct name) from table group by name;
用count函数试验一下也是可以的
select *, count(distinct name) from table group by name;
$sql = "UPDATE `{$table}` SET "; foreach ($val as $field){ $condition = " "; $sql .= "{$field} = CASE {$key}"; foreach ($data as $v) { $condition .= " WHEN {$v[$key]} THEN {$v[$field]}"; } $sql .= $condition . " END,"; } $sql=rtrim($sql,',') ." WHERE {$key} in ({$ids})"; try { $res = Yii::$app->db->createCommand($sql)->execute(); } catch (\Exception $e) { return [ "code" => 40073, "cn_msg" => "操作失败,请重新尝试或联系网站管理员。", "en_msg" => $e->getMessage(), ]; }
} return $res; } 例子 UPDATE categories SET display_order = CASE id WHEN 1 THEN 3 WHEN 2 THEN 4 WHEN 3 THEN 5 END, title = CASE id WHEN 1 THEN 'New Title 1' WHEN 2 THEN 'New Title 2' WHEN 3 THEN 'New Title 3' END WHERE id IN (1,2,3)
select a.user_id,count(a.user_id) as frequency from (select user_id from xxxx_attr_997_24281112 where attr_value='电商羽绒服' union all select user_id from xxxx_attr_908_24281111 where attr_value='电商羽绒服') as a group by a.user_id order by frequency desc