SELECT 
  cscart_products_categories.product_id, 
  GROUP_CONCAT(
    IF(
      cscart_products_categories.link_type = "M", 
      CONCAT(
        cscart_products_categories.category_id, 
        "M"
      ), 
      cscart_products_categories.category_id
    )
  ) AS category_ids 
FROM 
  cscart_products_categories 
  INNER JOIN cscart_categories ON cscart_categories.category_id = cscart_products_categories.category_id 
  AND cscart_categories.storefront_id IN (0, 1) 
  AND (
    cscart_categories.usergroup_ids = '' 
    OR FIND_IN_SET(
      0, cscart_categories.usergroup_ids
    ) 
    OR FIND_IN_SET(
      1, cscart_categories.usergroup_ids
    )
  ) 
  AND cscart_categories.status IN ('A', 'H') 
WHERE 
  cscart_products_categories.product_id IN (
    51165, 51164, 51163, 51162, 51161, 52, 
    51169, 51168, 51167, 51166, 90, 51171, 
    51170, 51176, 51174, 51173, 51172, 
    91, 51182, 51180, 51179, 51178, 51177, 
    93, 51183, 20, 51188, 51187, 51186, 
    51185, 51184, 51194, 51193, 51192, 
    51191, 51190, 51189, 19, 51201, 51200, 
    51199, 51198, 51197, 51196, 51195, 
    131, 51564, 51568, 51569, 51570, 51571, 
    51572, 51573, 51566, 51578, 51579, 
    51580, 51581
  ) 
GROUP BY 
  cscart_products_categories.product_id

Query time 0.00179

JSON explain

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "164.53"
    },
    "grouping_operation": {
      "using_temporary_table": true,
      "using_filesort": true,
      "cost_info": {
        "sort_cost": "26.39"
      },
      "nested_loop": [
        {
          "table": {
            "table_name": "cscart_categories",
            "access_type": "ALL",
            "possible_keys": [
              "PRIMARY",
              "c_status",
              "p_category_id"
            ],
            "rows_examined_per_scan": 383,
            "rows_produced_per_join": 15,
            "filtered": "4.00",
            "cost_info": {
              "read_cost": "40.09",
              "eval_cost": "1.53",
              "prefix_cost": "41.62",
              "data_read_per_join": "81K"
            },
            "used_columns": [
              "category_id",
              "storefront_id",
              "usergroup_ids",
              "status"
            ],
            "attached_condition": "((`test_uchur_k`.`cscart_categories`.`storefront_id` in (0,1)) and ((`test_uchur_k`.`cscart_categories`.`usergroup_ids` = '') or (0 <> find_in_set(0,`test_uchur_k`.`cscart_categories`.`usergroup_ids`)) or (0 <> find_in_set(1,`test_uchur_k`.`cscart_categories`.`usergroup_ids`))) and (`test_uchur_k`.`cscart_categories`.`status` in ('A','H')))"
          }
        },
        {
          "table": {
            "table_name": "cscart_products_categories",
            "access_type": "ref",
            "possible_keys": [
              "PRIMARY",
              "pt"
            ],
            "key": "PRIMARY",
            "used_key_parts": [
              "category_id"
            ],
            "key_length": "3",
            "ref": [
              "test_uchur_k.cscart_categories.category_id"
            ],
            "rows_examined_per_scan": 18,
            "rows_produced_per_join": 26,
            "filtered": "9.57",
            "index_condition": "(`test_uchur_k`.`cscart_products_categories`.`product_id` in (51165,51164,51163,51162,51161,52,51169,51168,51167,51166,90,51171,51170,51176,51174,51173,51172,91,51182,51180,51179,51178,51177,93,51183,20,51188,51187,51186,51185,51184,51194,51193,51192,51191,51190,51189,19,51201,51200,51199,51198,51197,51196,51195,131,51564,51568,51569,51570,51571,51572,51573,51566,51578,51579,51580,51581))",
            "cost_info": {
              "read_cost": "68.94",
              "eval_cost": "2.64",
              "prefix_cost": "138.14",
              "data_read_per_join": "422"
            },
            "used_columns": [
              "product_id",
              "category_id",
              "link_type"
            ]
          }
        }
      ]
    }
  }
}

Result

product_id category_ids
19 44,9,40M
20 43,9,40M
52 9,42,40M
90 43,9,40M
91 44,9,40M
93 42,9,40M
131 42,9,40M
51161 9,42,40M
51162 9,42,40M
51163 9,42,40M
51164 9,42,40M
51165 42,9,40M
51166 9,43,40M
51167 9,43,40M
51168 9,43,40M
51169 43,9,40M
51170 9,43,40M
51171 9,43,40M
51172 9,44,40M
51173 44,9,40M
51174 9,44,40M
51176 9,44,40M
51177 9,42,40M
51178 9,42,40M
51179 9,42,40M
51180 9,42,40M
51182 9,42,40M
51183 9,43,40M
51184 9,43,40M
51185 9,43,40M
51186 9,43,40M
51187 9,43,40M
51188 9,43,40M
51189 9,44,40M
51190 9,44,40M
51191 9,44,40M
51192 9,44,40M
51193 9,44,40M
51194 9,44,40M
51195 9,42,40M
51196 42,9,40M
51197 9,42,40M
51198 9,42,40M
51199 9,42,40M
51200 9,42,40M
51201 9,42,40M
51564 40,42,9M
51566 40,42,9M
51568 40,42,9M
51569 42,40,9M
51570 42,40,9M
51571 40,42,9M
51572 40,42,9M
51573 42,40,9M
51578 42,40,9M
51579 42,40,9M
51580 40,42,9M
51581 40,42,9M