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, 
  product_position_source.position AS position 
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') 
  LEFT JOIN cscart_products_categories AS product_position_source ON cscart_products_categories.product_id = product_position_source.product_id 
  AND product_position_source.category_id = 40 
WHERE 
  cscart_products_categories.product_id IN (
    51567, 51480, 51565, 51564, 51479, 139, 
    129, 51566, 51301, 51494, 51, 50, 51497, 
    51498, 51495, 51563, 53, 22713, 21, 
    22714, 22712, 46, 22715, 22716, 51496, 
    21567, 91, 50989, 105, 48, 49, 92, 137, 
    88, 126, 31, 51358, 115, 80, 37, 11, 140, 
    41, 30, 90, 38, 124, 81, 56, 82, 116, 117, 
    125, 106, 39, 193, 40, 22710, 22711, 
    17, 57, 122, 58, 5
  ) 
GROUP BY 
  cscart_products_categories.product_id

Query time 0.00341

JSON explain

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "179.57"
    },
    "grouping_operation": {
      "using_temporary_table": true,
      "using_filesort": true,
      "cost_info": {
        "sort_cost": "30.69"
      },
      "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": 30,
            "filtered": "11.13",
            "index_condition": "(`test_uchur_k`.`cscart_products_categories`.`product_id` in (51567,51480,51565,51564,51479,139,129,51566,51301,51494,51,50,51497,51498,51495,51563,53,22713,21,22714,22712,46,22715,22716,51496,21567,91,50989,105,48,49,92,137,88,126,31,51358,115,80,37,11,140,41,30,90,38,124,81,56,82,116,117,125,106,39,193,40,22710,22711,17,57,122,58,5))",
            "cost_info": {
              "read_cost": "68.94",
              "eval_cost": "3.07",
              "prefix_cost": "138.14",
              "data_read_per_join": "491"
            },
            "used_columns": [
              "product_id",
              "category_id",
              "link_type"
            ]
          }
        },
        {
          "table": {
            "table_name": "product_position_source",
            "access_type": "eq_ref",
            "possible_keys": [
              "PRIMARY",
              "pt"
            ],
            "key": "PRIMARY",
            "used_key_parts": [
              "category_id",
              "product_id"
            ],
            "key_length": "6",
            "ref": [
              "const",
              "test_uchur_k.cscart_products_categories.product_id"
            ],
            "rows_examined_per_scan": 1,
            "rows_produced_per_join": 30,
            "filtered": "100.00",
            "cost_info": {
              "read_cost": "7.67",
              "eval_cost": "3.07",
              "prefix_cost": "148.88",
              "data_read_per_join": "491"
            },
            "used_columns": [
              "product_id",
              "category_id",
              "position"
            ]
          }
        }
      ]
    }
  }
}

Result

product_id category_ids position
5 6,40M,42 0
11 8,40M,42,394 0
17 6,40M,44 0
21 10,40M,43 0
30 18,40M,44,391 0
31 18,40M,43,391 0
37 10,40M,42 0
38 10,40M,42 0
39 10,40M,42 0
40 10,40M,44 0
41 10,40M,43 0
46 18,40M,42,391 0
48 10,40M,44 0
49 10,40M,43 0
50 23,40M,44 0
51 23,40M,43 0
53 10,40M,44 0
56 8,40M,42,394 0
57 6,40M,44 0
58 6,40M,43 0
80 26,40M 0
81 8,40M,43,394 0
82 8,40M,44,394 0
88 40M,42,235 0
90 9,40M,43 0
91 9,40M,44 0
92 10,40M,42 0
105 6,40M,44,394 0
106 6,40M,43,394 0
115 8,40M,42,394 0
116 8,40M,42,394 0
117 8,40M,42,394 0
122 6,40M,42 0
124 7,40M,42 0
125 7,40M,42 0
126 8,40M,42,394 0
129 24,40M,42 0
137 7,40M,42 0
139 24,40M,42 0
140 25,40M,42 0
193 9,40M,43 0
21567 29M,40,42,394 0
22710 18M,40,42,392 0
22711 18M,40,42,392 0
22712 7M,40,42 0
22713 7M,40,42 0
22714 7M,40,42 0
22715 7M,40,42 0
22716 7M,40,42 0
50989 29M,40,42,394 0
51301 24,40M,42 0
51358 18,40M,42,391 0
51479 4M,40,42 0
51480 4M,40,42 0
51494 40,42,204M 0
51495 40M,42,204 0
51496 33M,40 0
51497 33M,40 0
51498 33M,40 0
51563 1M,40,42 0
51564 9M,40,42 0
51565 10,40M,42 0
51566 9M,40,42 0
51567 10M,40,42 0