简体   繁体   中英

MySQL Count presence of multiple values in same column

I have a single table called Customers with a CustID column and another column called ProductCode. There are 33 possible products (numbered 1 to 33) and each customer can have any combination of products. There is a single row for each customer and all of the products that each customer has are shown in the ProductCode column with a space in between ie:

CUSTID     PRODUCTCODE  
------     -----------

001        3 12 18 22 32  
002        9 6 18 36  
003        3 6 7 26  
004        9 11 33   
005        6 21 28 29 30 31  
006        1 3 6 21 30 31  

I need to be able to do a count of each product and add a text description of the code:

ProdCode    Description       Count  
~~~~~~~~    ~~~~~~~~~~~       ~~~~~  
1           Lawnmower         1  
3           Spade             3  
6           Clippers          4  
etc

Thanks!

Never, never, never store multiple values in one column!

As you see now this will only result in problems. Please normalize your DB structure with an additional table

customer_products
CUSTID     PRODUCTCODE  
------     -----------
001        3
001        12
001        18
---
002        9
002        6
...

Then you can remove the PRODUCTCODE column from your customer table.

One solution can be to change you schema to have two columns:-

1) Customer Id 2) Product Id

So data would look something like this:-

cust_id| product_id

001    | 3
001    | 6
002    | 9

... ...

So now in mysql query you can use query like

Select product_id,count(*) as num_products from table_name group by product_id;

Hope this helps.

If you cant alter your schema, then you have to fetch the data and in your scripting language you have to process it as you want.

Try this:

CREATE TABLE ord (custid VARCHAR(3), productcode VARCHAR(40));

INSERT INTO ord VALUES ('001', '3 12 18 22 32');
INSERT INTO ord VALUES ('002', '9 6 18 36');
INSERT INTO ord VALUES ('003', '3 6 7 26');
INSERT INTO ord VALUES ('004', '9 11 33');
INSERT INTO ord VALUES ('005', '6 21 28 29 30 31');
INSERT INTO ord VALUES ('006', '1 3 6 21 30 31');

CREATE TABLE products (code VARCHAR(2));

INSERT INTO products VALUES ('3');
INSERT INTO products VALUES ('12');
INSERT INTO products VALUES ('18');
INSERT INTO products VALUES ('22');
INSERT INTO products VALUES ('32');
INSERT INTO products VALUES ('9');
INSERT INTO products VALUES ('6');
INSERT INTO products VALUES ('31');

CREATE TABLE ord_prod (code VARCHAR(2), count_of_products INT) AS
  SELECT p.code, 
        (SELECT COUNT(1)
           FROM ord o
         WHERE INSTR(CONCAT(' ', o.productcode, ' '), CONCAT(' ', p.code, ' ')) > 0) AS count_of_products
    FROM products p;

Check at SQLFiddle: http://sqlfiddle.com/#!2/a7f94/1

Change your types as you need, add the rest of the products, add the description etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM