MySQL bit_or () bit_count () function

Source: Internet
Author: User

meaning of the Bit_count function

Used to calculate the number of binary numbers that contain 1.

Select Bit_count (10);
Because 10 turns into binary is 1010, so the result is 2.

meaning of the Bit_or function

is to perform or operate on two binary numbers. Such as:

    1100 or  0101--------------    1101


Example of the MySQL website manual, using Bit_or () Bit_count () to subtly filter out duplicate dates:

The following example shows how can I use the bit group functions to calculate the number of days per month a user have V isited a Web page.

CREATE TABLE T1 (Year of year (4), month int (2) UNSIGNED Zerofill, day             int (2) UNSIGNED Zerofill), INSERT into T1 VALUES (200 0,1,1), (2000,1,20), (2000,1,30), (2000,2,2),            (2000,2,23), (2000,2,23);

The example table contains Year-month-day values representing visits by users to the page. To determine how many different days in each month these visits occur, use this query:

SELECT Year,month,bit_count (Bit_or (1<<day)) as days from T1       GROUP by Year,month;

which returns:

+------+-------+------+| Year | Month | Days |+------+-------+------+| |    |    3 | | |    |    2 |+------+-------+------+

MySQL bit_or () bit_count () function

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.