How does PHPmysql query the number of different values of a field?

Source: Internet
Author: User
How does PHPmysql query the number of different values of a field? for example,
The car table has a pinpai field,
There are 6 Lambo, Audi, BMW, Audi, Toyota, and Lambo values,
This field has four different brands and six vehicles.
How can we obtain the value 4 and the value 6?
The front-end shows: "There are a total of 6 vehicles of four brands. "
-------------------------------------------------------------------------

Please give me a complete SQL select statement. I am a newbie, practical, and experienced driver.
--------------------------------------------------------------------------
Do not copy and paste
Select pinpai from car group by pinpai and select distinct pinpai from car
It's not what I want to answer, because they lack quantity.


Reply to discussion (solution)

create temporary table car (pinpai varchar(10));insert into car values ('Lambo'),('Audi'),('BMW'),('Audi'),('Toyota'),('Lambo');select pinpai, count(*) as cnt from car group by 1
pinpai cnt Audi   2 BMW    1 Lambo  2 Toyota 1 

You don't understand what I mean. you are not the result. I want to display the display at the front desk: "There are four types of vehicles, a total of six. "Instead of displaying the number of each brand

create temporary table car (pinpai varchar(10));insert into car values ('Lambo'),('Audi'),('BMW'),('Audi'),('Toyota'),('Lambo');select pinpai, count(*) as cnt from car group by 1
pinpai cnt Audi   2 BMW    1 Lambo  2 Toyota 1 

You don't understand what I mean. you are not the result. I want to display the display at the front desk: "There are four types of vehicles, a total of six. "Instead of displaying the number of each brand

Use group by + count.

Select pinpai, count (*) as num from car group by pinpai;

select count(*) pinpai, sum(cnt) as cnt  from (select pinpai, count(*) as cnt from car group by 1) T
pinpai cnt 4      6 

You do not know any database knowledge at all?

select count(*) pinpai, sum(cnt) as cnt  from (select pinpai, count(*) as cnt from car group by 1) T
pinpai cnt 4      6 

You do not know any database knowledge at all?

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.