Simple mysql packet statistics statement instance code

Source: Internet
Author: User
Keywords Network programming Mysql tutorial
Tags code how many mysql mysql tutorial name network network programming programming

Simple mysql tutorial grouping statistics statement instance code
/ *
id name code_id code_name is_true score
1 Zhang San 1 handsome is 5
2 Zhang San 2 lavish is 4
3 Zhang San 1 handsome is 4
4 Zhang San 2 lavish no 3
5 Li Si 1 handsome is 5
6 Li Si 2 lavish is 3

Look at an example
mysql how to group statistics by name: [handsome] how many "yes" and "no", "lavish" how many "yes" and "no", and how many [score]

* /

select name,
sum (if (code_name = 'handsome' and is_true = 'yes', 1,0)),
sum (if (code_name = 'handsome' and is_true = 'no', 1,0)),
sum (if (code_name = 'lazy' and is_true = 'yes', 1,0)),
sum (if (code_name = 'lazy' and is_true = 'no', 1,0)),
count (*)
from table1
group by name

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.