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 (
    128, 51118, 51117, 51116, 51115, 51114, 
    51113, 51165, 51164, 51163, 51162, 
    51161, 52, 51183, 20, 51188, 51187, 
    51186, 51185, 51184, 51201, 51200, 
    51199, 51198, 51197, 51196, 51195, 
    131, 51220, 51219, 51218, 51217, 51216, 
    146, 51448, 51449, 51231, 51230, 51229, 
    51228, 51227, 51226, 51225, 109, 51243, 
    51242, 51241, 51240, 51239, 36, 51273, 
    147, 51277, 51276, 51275, 51274, 16, 
    114, 84, 51352, 51351, 96, 89, 51390, 
    51391, 51392, 51393, 51394
  ) 
GROUP BY 
  cscart_products_categories.product_id

Query time 0.00236

JSON explain

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "169.57"
    },
    "grouping_operation": {
      "using_temporary_table": true,
      "using_filesort": true,
      "cost_info": {
        "sort_cost": "31.43"
      },
      "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": 31,
            "filtered": "11.40",
            "index_condition": "(`test_uchur_k`.`cscart_products_categories`.`product_id` in (128,51118,51117,51116,51115,51114,51113,51165,51164,51163,51162,51161,52,51183,20,51188,51187,51186,51185,51184,51201,51200,51199,51198,51197,51196,51195,131,51220,51219,51218,51217,51216,146,51448,51449,51231,51230,51229,51228,51227,51226,51225,109,51243,51242,51241,51240,51239,36,51273,147,51277,51276,51275,51274,16,114,84,51352,51351,96,89,51390,51391,51392,51393,51394))",
            "cost_info": {
              "read_cost": "68.94",
              "eval_cost": "3.14",
              "prefix_cost": "138.14",
              "data_read_per_join": "502"
            },
            "used_columns": [
              "product_id",
              "category_id",
              "link_type"
            ]
          }
        }
      ]
    }
  }
}

Result

product_id category_ids
16 6,44,40M
20 43,9,40M
36 43,7,40M
52 9,42,40M
84 42,28,394,40M
89 25,42,40M
96 42,32,40M
109 42,7,40M
114 6,42,40M
128 43,10,40M
131 42,9,40M
146 42,7,390,40M
147 7,42,40M
51113 10,43,40M
51114 10,43,40M
51115 10,43,40M
51116 10,43,40M
51117 10,43,40M
51118 10,43,40M
51161 9,42,40M
51162 9,42,40M
51163 9,42,40M
51164 9,42,40M
51165 42,9,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
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
51216 390,42,7,40M
51217 390,42,7,40M
51218 390,42,7,40M
51219 390,42,7,40M
51220 390,42,7,40M
51225 7,42,40M
51226 7,42,40M
51227 7,42,40M
51228 7,42,40M
51229 7,42,40M
51230 7,42,40M
51231 7,42,40M
51239 7,43,40M
51240 7,43,40M
51241 7,43,40M
51242 7,43,40M
51243 7,43,40M
51273 7,42,40M
51274 42,7,40M
51275 42,7,40M
51276 42,7,40M
51277 42,7,40M
51351 32,42,40M
51352 32,42,40M
51390 42,25,40M
51391 42,25,40M
51392 42,25,40M
51393 42,25,40M
51394 25,42,40M
51448 390,7,42,40M
51449 390,7,42,40M