Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8939661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:52:05+00:00 2026-06-15T10:52:05+00:00

i have the following, set of array and a mysql table ,table1 $arr1 =

  • 0

i have the following, set of array and a mysql table ,table1

$arr1 = (“A0″,”A1″,”A2″,”A3″,”A4”)
$arr2 = (“B0″,”B1″,”B2″,”B3″,”B4”)

+----+-----------------+------+------+
| id | Col1            | Col2 | Col3 |
+----+-----------------+------+------+
|  0 | A0;B1;B2;       | x    | 9    |
|  1 | A0;B1;B2;A1;A2; | x    | 15   |
|  2 | A0;             | x    | 7    |
|  3 | B0;             | x    | 5    |
|  4 | C0;             | j    | 5    |
+----+-----------------+------+------+

is it possible that i can query the values in my table so that the final output will be something like this

    +----+-------+------+
    | id |  C31T | C32T |
    +----+-------+------+
    |  0 |  19   |  17  |
    +----+-------+------+

C31T and C32T was from this table

    +----+------+------ +-------+------+------+
    | id | Arr1 | Arr2  |  C31  | C32  | tot  |
    +----+------+-------+-------+------+------+
    |  0 | 1    | 2     |  3    |  6   | 3    |
    |  1 | 3    | 2     |  9    |  6   | 5    |
    |  2 | 1    | 0     |  7    |  0   | 1    |
    |  3 | 0    | 1     |  0    |  5   | 1    |
    +----+------+-------+-------+------+------+

Following eggyal solution i’m stuck up to this point

    SELECT   table1.id,
         COUNT(DISTINCT arr1.element) AS Arr1,
         COUNT(DISTINCT arr2.element) AS Arr2,
         COUNT(DISTINCT arr1.element) +
         COUNT(DISTINCT arr2.element) AS tot,
(COUNT(DISTINCT arr1.element)/(COUNT(DISTINCT arr1.element)+COUNT(DISTINCT arr2.element)))*col3 AS c31,
(COUNT(DISTINCT arr2.element)/(COUNT(DISTINCT arr1.element)+COUNT(DISTINCT arr2.element)))*col3 AS c32
FROM     table1
  LEFT JOIN (
    SELECT 'A0' AS element
    UNION ALL SELECT 'A1'
    UNION ALL SELECT 'A2'
    UNION ALL SELECT 'A3'
    UNION ALL SELECT 'A4'
  ) arr1 ON FIND_IN_SET(
    arr1.element,
    REPLACE(table1.Col1, ';', ',')
  )
  LEFT JOIN (
    SELECT 'B0' AS element
    UNION ALL SELECT 'B1'
    UNION ALL SELECT 'B2'
    UNION ALL SELECT 'B3'
    UNION ALL SELECT 'B4'
  ) arr2 ON FIND_IN_SET(
    arr2.element,
    REPLACE(table1.Col1, ';', ',')
  )
WHERE    table1.Col2 = 'x'
GROUP BY table1.id
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:52:06+00:00Added an answer on June 15, 2026 at 10:52 am

    I completely agree with @Kickstart’s comment—you really should normalise your schema:

    CREATE TABLE associations (
      id      INT,
      element VARCHAR(2),
      FOREIGN KEY (id) REFERENCES table1 (id)
    );
    
    INSERT INTO associations
      (id, element)
    VALUES
      (0, 'A0'), (0, 'B1'), (0, 'B2'),
      (1, 'A0'), (1, 'B1'), (1, 'B2'), (1, 'A1'), (1, 'A2'),
      (2, 'A0'),
      (3, 'B0'),
      (4, 'C0')
    ;
    
    ALTER TABLE table1 DROP Col1;
    

    Then your query would be:

    SELECT   table1.id,
             COUNT(DISTINCT arr1.element) AS Arr1,
             COUNT(DISTINCT arr2.element) AS Arr2,
             COUNT(DISTINCT arr1.element) +
             COUNT(DISTINCT arr2.element) AS tot
    FROM     table1 JOIN associations USING (id)
      LEFT JOIN (
        SELECT 'A0' AS element
        UNION ALL SELECT 'A1'
        UNION ALL SELECT 'A2'
        UNION ALL SELECT 'A3'
        UNION ALL SELECT 'A4'
      ) arr1 USING (element)
      LEFT JOIN (
        SELECT 'B0' AS element
        UNION ALL SELECT 'B1'
        UNION ALL SELECT 'B2'
        UNION ALL SELECT 'B3'
        UNION ALL SELECT 'B4'
      ) arr2 USING (element)
    WHERE    table1.Col2 = 'x'
    GROUP BY table1.id
    

    Results:

    | ID | ARR1 | ARR2 | TOT |
    --------------------------
    |  0 |    1 |    2 |   3 |
    |  1 |    3 |    2 |   5 |
    |  2 |    1 |    0 |   1 |
    |  3 |    0 |    1 |   1 |
    

    Without such a normalised schema, the “quite hideous coding” to which (s)he referred would be:

    SELECT   table1.id,
             COUNT(DISTINCT arr1.element) AS Arr1,
             COUNT(DISTINCT arr2.element) AS Arr2,
             COUNT(DISTINCT arr1.element) +
             COUNT(DISTINCT arr2.element) AS tot
    FROM     table1
      LEFT JOIN (
        SELECT 'A0' AS element
        UNION ALL SELECT 'A1'
        UNION ALL SELECT 'A2'
        UNION ALL SELECT 'A3'
        UNION ALL SELECT 'A4'
      ) arr1 ON FIND_IN_SET(
        arr1.element,
        REPLACE(table1.Col1, ';', ',')
      )
      LEFT JOIN (
        SELECT 'B0' AS element
        UNION ALL SELECT 'B1'
        UNION ALL SELECT 'B2'
        UNION ALL SELECT 'B3'
        UNION ALL SELECT 'B4'
      ) arr2 ON FIND_IN_SET(
        arr2.element,
        REPLACE(table1.Col1, ';', ',')
      )
    WHERE    table1.Col2 = 'x'
    GROUP BY table1.id
    

    Results:

    | ID | ARR1 | ARR2 | TOT |
    --------------------------
    |  0 |    1 |    2 |   3 |
    |  1 |    3 |    2 |   5 |
    |  2 |    1 |    0 |   1 |
    |  3 |    0 |    1 |   1 |
    

    UPDATE

    Following your edit, you merely need to perform an outer query on this existing one:

    SELECT SUM(Arr1/tot) AS C31T, SUM(Arr2/tot) AS C32T
    FROM (
    
      SELECT   COUNT(DISTINCT arr1.element) * table1.Col3 AS Arr1,
               COUNT(DISTINCT arr2.element) * table1.Col3 AS Arr2,
               COUNT(DISTINCT arr1.element) +
               COUNT(DISTINCT arr2.element) AS tot
      FROM     table1
        LEFT JOIN (
          SELECT 'A0' AS element
          UNION ALL SELECT 'A1'
          UNION ALL SELECT 'A2'
          UNION ALL SELECT 'A3'
          UNION ALL SELECT 'A4'
        ) arr1 ON FIND_IN_SET(
          arr1.element,
          REPLACE(table1.Col1, ';', ',')
        )
        LEFT JOIN (
          SELECT 'B0' AS element
          UNION ALL SELECT 'B1'
          UNION ALL SELECT 'B2'
          UNION ALL SELECT 'B3'
          UNION ALL SELECT 'B4'
        ) arr2 ON FIND_IN_SET(
          arr2.element,
          REPLACE(table1.Col1, ';', ',')
        )
      WHERE    table1.Col2 = 'x'
      GROUP BY table1.id
    
    ) t
    

    Results:

    | C31T | C32T |
    ---------------
    |   19 |   17 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL table set up using phpMyAdmin which you can view in
I have a rather large mysql table that has the following structure: fieldid |
I have the following query: SET @q = 12; UPDATE `table` SET qty =
I have MySQL table with the following structure: +---------+--------------+------+-----+---------+----------------+ | Field | Type |
I have a table of order statuses that a sales order can have; the
I have the following table my_table with primary key id set to AUTO_INCREMENT. id
I have the following MySQL tables: CREATE TABLE IF NOT EXISTS `conversations` ( `id`
I have the following set of data: ID | CREATED | USER -------------------------- 1
I'm trying to map a Dictionary containing Lists. I have the following set of
I'm using log4Net for my logging. I also have the following set... <log4net debug=true>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.