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 (
    51076, 51073, 51072, 51074, 51075, 22711, 
    22710, 51077, 51080, 51078, 51079, 
    51139, 51138, 51137, 51136, 51135, 
    40, 51144, 51143, 51142, 51141, 51140, 
    39, 51205, 51204, 51203, 51202, 125, 
    27, 22, 17, 18, 106, 58, 57, 5, 51349, 
    122, 65
  ) 
GROUP BY 
  cscart_products_categories.product_id

Query time 0.00157

JSON explain

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "111.36"
    },
    "grouping_operation": {
      "using_filesort": false,
      "nested_loop": [
        {
          "table": {
            "table_name": "cscart_products_categories",
            "access_type": "range",
            "possible_keys": [
              "PRIMARY",
              "pt"
            ],
            "key": "pt",
            "used_key_parts": [
              "product_id"
            ],
            "key_length": "3",
            "rows_examined_per_scan": 127,
            "rows_produced_per_join": 127,
            "filtered": "100.00",
            "index_condition": "(`test_uchur_k`.`cscart_products_categories`.`product_id` in (51076,51073,51072,51074,51075,22711,22710,51077,51080,51078,51079,51139,51138,51137,51136,51135,40,51144,51143,51142,51141,51140,39,51205,51204,51203,51202,125,27,22,17,18,106,58,57,5,51349,122,65))",
            "cost_info": {
              "read_cost": "54.21",
              "eval_cost": "12.70",
              "prefix_cost": "66.91",
              "data_read_per_join": "1K"
            },
            "used_columns": [
              "product_id",
              "category_id",
              "link_type"
            ]
          }
        },
        {
          "table": {
            "table_name": "cscart_categories",
            "access_type": "eq_ref",
            "possible_keys": [
              "PRIMARY",
              "c_status",
              "p_category_id"
            ],
            "key": "PRIMARY",
            "used_key_parts": [
              "category_id"
            ],
            "key_length": "3",
            "ref": [
              "test_uchur_k.cscart_products_categories.category_id"
            ],
            "rows_examined_per_scan": 1,
            "rows_produced_per_join": 6,
            "filtered": "5.00",
            "cost_info": {
              "read_cost": "31.75",
              "eval_cost": "0.64",
              "prefix_cost": "111.36",
              "data_read_per_join": "33K"
            },
            "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')))"
          }
        }
      ]
    }
  }
}

Result

product_id category_ids
5 6,42,40M
17 6,44,40M
18 6,43,40M
22 6,42,40M
27 6,42,40M
39 10,42,40M
40 10,44,40M
57 6,44,40M
58 6,43,40M
65 45,25M
106 43,6,394,40M
122 42,6,40M
125 42,7,40M
22710 392,42,40,18M
22711 392,42,40,18M
51072 40,42,392,18M
51073 40,42,392,18M
51074 40,42,392,18M
51075 40,42,392,18M
51076 392,40,42,18M
51077 392,40,42,18M
51078 392,40,42,18M
51079 392,40,42,18M
51080 392,40,42,18M
51135 44,10,40M
51136 44,10,40M
51137 44,10,40M
51138 44,10,40M
51139 44,10,40M
51140 42,10,40M
51141 42,10,40M
51142 42,10,40M
51143 10,42,40M
51144 42,10,40M
51202 7,42,40M
51203 7,42,40M
51204 7,42,40M
51205 7,42,40M
51349 42,6,40M