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 (
    21567, 51083, 22715, 51082, 51085, 51081, 
    51096, 51097, 51098, 51100, 51099, 
    22716, 51123, 51122, 51121, 92, 49, 
    51129, 51128, 51127, 51126, 51125, 
    48, 51176, 51174, 51173, 51172, 91, 
    51221, 137, 51224, 51223, 126, 51343, 
    51342, 51341, 51340, 47, 105, 50989, 
    31, 51366, 51367, 51368, 51369, 46, 
    51370, 51371, 51372, 51373, 51452, 
    51453, 88, 51395, 51396, 51397, 51555, 
    51556, 51496
  ) 
GROUP BY 
  cscart_products_categories.product_id

Query time 0.00195

JSON explain

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "165.56"
    },
    "grouping_operation": {
      "using_temporary_table": true,
      "using_filesort": true,
      "cost_info": {
        "sort_cost": "27.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": 27,
            "filtered": "9.95",
            "index_condition": "(`test_uchur_k`.`cscart_products_categories`.`product_id` in (21567,51083,22715,51082,51085,51081,51096,51097,51098,51100,51099,22716,51123,51122,51121,92,49,51129,51128,51127,51126,51125,48,51176,51174,51173,51172,91,51221,137,51224,51223,126,51343,51342,51341,51340,47,105,50989,31,51366,51367,51368,51369,46,51370,51371,51372,51373,51452,51453,88,51395,51396,51397,51555,51556,51496))",
            "cost_info": {
              "read_cost": "68.94",
              "eval_cost": "2.74",
              "prefix_cost": "138.14",
              "data_read_per_join": "438"
            },
            "used_columns": [
              "product_id",
              "category_id",
              "link_type"
            ]
          }
        }
      ]
    }
  }
}

Result

product_id category_ids
31 43,391,18,40M
46 42,18,391,40M
47 45,23M
48 10,44,40M
49 10,43,40M
88 235,42,40M
91 44,9,40M
92 42,10,40M
105 44,6,394,40M
126 42,8,394,40M
137 42,7,40M
21567 394,42,40,29M
22715 42,40,7M
22716 42,40,7M
50989 394,42,40,29M
51081 40,42,7M
51082 42,40,7M
51083 40,42,7M
51085 40,42,7M
51096 40,42,7M
51097 40,42,7M
51098 40,42,7M
51099 40,42,7M
51100 40,42,7M
51121 10,42,40M
51122 10,42,40M
51123 10,42,40M
51125 10,44,40M
51126 44,10,40M
51127 44,10,40M
51128 44,10,40M
51129 44,10,40M
51172 9,44,40M
51173 44,9,40M
51174 9,44,40M
51176 9,44,40M
51221 7,42,40M
51223 7,42,40M
51224 7,42,40M
51340 45,23M
51341 45,23M
51342 45,23M
51343 45,23M
51366 18,391,43,40M
51367 391,43,18,40M
51368 391,43,18,40M
51369 391,43,18,40M
51370 391,18,42,40M
51371 391,18,42,40M
51372 391,18,42,40M
51373 391,18,42,40M
51395 235,42,40M
51396 42,235,40M
51397 235,42,40M
51452 18,42,391,40M
51453 18,42,391,40M
51496 40,33M
51555 42,235,40M
51556 235,42,40M