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.company_id = 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 = 345 
WHERE 
  cscart_products_categories.product_id IN (
    421, 427, 408, 382, 418, 1930, 400, 424, 
    1931, 381, 1529, 401, 1465, 1932, 1933, 
    1935, 428, 415, 391, 406, 439, 390, 393, 
    394, 1929, 396, 397, 398, 399, 422, 434, 
    383, 412, 1927, 419, 430, 431, 432, 1928, 
    429, 384, 1934, 395, 420, 385, 405, 410, 
    413, 417, 1937, 414, 425, 407, 389, 402, 
    388, 426, 409, 404, 1936, 387, 386, 416, 
    441, 322
  ) 
GROUP BY 
  cscart_products_categories.product_id

Query time 0.01699

JSON explain

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "230.94"
    },
    "grouping_operation": {
      "using_temporary_table": true,
      "using_filesort": true,
      "cost_info": {
        "sort_cost": "42.05"
      },
      "nested_loop": [
        {
          "table": {
            "table_name": "cscart_categories",
            "access_type": "ALL",
            "possible_keys": [
              "PRIMARY",
              "c_status",
              "p_category_id"
            ],
            "rows_examined_per_scan": 115,
            "rows_produced_per_join": 2,
            "filtered": "2.00",
            "cost_info": {
              "read_cost": "27.56",
              "eval_cost": "0.46",
              "prefix_cost": "28.02",
              "data_read_per_join": "6K"
            },
            "used_columns": [
              "category_id",
              "company_id",
              "usergroup_ids",
              "status"
            ],
            "attached_condition": "((`eshopuga_upgr_4122`.`cscart_categories`.`company_id` = 1) and ((`eshopuga_upgr_4122`.`cscart_categories`.`usergroup_ids` = '') or find_in_set(0,`eshopuga_upgr_4122`.`cscart_categories`.`usergroup_ids`) or find_in_set(1,`eshopuga_upgr_4122`.`cscart_categories`.`usergroup_ids`)) and (`eshopuga_upgr_4122`.`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": [
              "eshopuga_upgr_4122.cscart_categories.category_id"
            ],
            "rows_examined_per_scan": 40,
            "rows_produced_per_join": 42,
            "filtered": "45.71",
            "index_condition": "(`eshopuga_upgr_4122`.`cscart_products_categories`.`product_id` in (421,427,408,382,418,1930,400,424,1931,381,1529,401,1465,1932,1933,1935,428,415,391,406,439,390,393,394,1929,396,397,398,399,422,434,383,412,1927,419,430,431,432,1928,429,384,1934,395,420,385,405,410,413,417,1937,414,425,407,389,402,388,426,409,404,1936,387,386,416,441,322))",
            "cost_info": {
              "read_cost": "92.00",
              "eval_cost": "8.41",
              "prefix_cost": "138.42",
              "data_read_per_join": "672"
            },
            "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",
              "eshopuga_upgr_4122.cscart_products_categories.product_id"
            ],
            "rows_examined_per_scan": 1,
            "rows_produced_per_join": 42,
            "filtered": "100.00",
            "cost_info": {
              "read_cost": "42.05",
              "eval_cost": "8.41",
              "prefix_cost": "188.89",
              "data_read_per_join": "672"
            },
            "used_columns": [
              "product_id",
              "category_id",
              "position"
            ]
          }
        }
      ]
    }
  }
}

Result

product_id category_ids position
322 352,375,376,357,371,377,372,356,339,378,374,355,344,358,345,349M,370,350,351,373,369,354,341,342,353,338 0
381 341,342,361,353,338,352,383,375,360,376,386,357,371,384,380,372,379,356,339,340,359,378,374,385,355,344,358,345,347,381,349,343,370,382,350,351,373,354,362,363,337M 0
382 370,382,350,351,373,354,337M,341,342,353,338,352,383,375,377,376,386,357,371,384,372,379,356,339,340,378,374,385,355,344,369,358,345,347,381,349,343 0
383 378,374,385,355,344,358,345,347,368,381,362,363,349,343,370,350,351,373,354,361,337M,341,342,360,353,338,352,375,377,376,386,357,371,384,359,372,356,339,340 0
384 356,339,340,355,344,358,345,347,349,343,350,351,354,337M,341,342,353,338,352,357 0
385 369,393,378,374,388,356,339,340,368,362,363,370,355,344,373,358,345,347,361,349,343,350,351,394,360,354,337M,341,395,342,375,392,377,353,338,376,391,352,371,390,359,372,396,357 0
386 357,356,339,340,355,344,358,345,347,349,343,350,351,354,337M,341,342,353,338,352 0
387 342,353,338,352,357,356,339,340,355,344,358,345,347,349,343,350,351,354,337M,341 0
388 354,337M,341,359,385,342,382,353,338,381,352,368,362,363,357,383,361,380,356,339,340,360,355,386,344,358,379,384,345,347,349,343,350,351 0
389 358,371,379,384,345,347,349,343,372,350,351,354,369,337M,341,378,374,385,342,382,353,338,381,352,370,373,357,383,380,356,375,339,340,377,355,376,386,344 0
390 356,339,340,390,355,396,344,358,345,347,393,349,343,359,350,351,388,354,337M,341,342,353,338,368,362,363,352,394,361,357,395,392,360,391 0
391 356,339,340,355,344,358,345,347,349,343,350,351,354,337M,341,342,353,338,352,357 0
393 375,377,357,376,386,371,379,384,372,356,339,340,369,355,344,378,374,385,358,382,345,347,349,343,381,350,351,370,354,373,337M,341,342,383,353,338,352,380 0
394 383,353,338,352,380,375,377,357,376,386,371,379,384,372,356,339,340,369,355,344,378,374,385,358,382,345,347,349,343,381,350,351,370,354,373,337M,341,342 0
395 337M,341,342,353,338,352,357,356,339,340,355,344,358,345,347,349,343,350,351,354 0
396 373,349,343,350,351,354,337M,341,342,353,338,375,377,352,376,371,357,372,369,378,374,356,339,340,355,344,370,358,345,347 0
397 355,344,358,345,347,349,343,350,351,354,337M,341,342,353,338,352,357,356,339,340 0
398 395,392,360,356,391,339,340,355,390,344,358,396,345,347,349,343,393,350,351,359,354,388,337M,341,342,389,353,338,352,368,362,363,357,394,361 0
399 357,356,339,340,355,344,358,345,347,349,343,350,351,354,337M,341,342,353,338,352 0
400 352,357,356,339,340,355,344,358,345,347,349,343,350,351,354,337M,341,342,353,338 0
401 342,338,352,339,340,344,358,345,349,343,351,337M,341 0
402 351,394,361,353,337M,341,395,392,342,360,338,391,357,352,390,396,393,359,356,388,355,347,339,340,350,344,358,368,362,363,345,354,349,343 0
404 350,344,358,345,354,349,343,351,353,337M,341,342,338,357,352,356,355,347,339,340 0
405 368,362,363,347,339,340,370,373,350,344,394,358,361,345,354,349,343,395,351,392,360,353,337M,391,341,375,342,377,390,338,376,396,357,352,371,393,359,372,388,369,356,378,374,355 0
406 356,355,368,362,363,347,339,340,350,344,394,358,361,345,354,349,343,395,351,392,360,353,337M,391,341,342,390,338,396,357,352,393,359,388 0
407 393,359,388,383,356,380,355,368,362,363,347,339,340,386,350,344,379,384,394,358,361,345,354,349,343,395,351,392,360,353,337M,391,341,385,382,342,390,338,381,396,357,352 0
408 369,338,381,357,378,374,352,370,373,383,356,380,355,347,339,340,386,350,344,375,379,384,358,377,345,354,349,343,376,351,371,353,337M,372,341,385,382,342 0
409 353,337M,341,342,357,352,356,355,347,339,340,350,344,358,345,354,349,343,351 0
410 358,345,354,349,343,351,353,338,337M,341,342,357,352,356,355,339,340,350,344 0
412 339,340,350,344,358,345,354,349,343,351,353,338,337M,341,342,357,352,356,347,355 0
413 372,355,369,339,340,359,378,374,350,344,358,345,354,349,343,370,351,373,353,338,337M,368,362,363,341,342,361,357,352,375,360,377,376,371,356 0
414 377,376,371,356,372,355,369,339,340,359,378,374,350,344,358,345,354,349,343,370,351,373,353,338,337M,368,362,363,341,342,361,357,352,375,360 0
415 357,352,356,355,339,340,350,344,358,345,354,349,343,351,353,338,337M,341,342 0
416 361,390,341,396,342,360,393,357,352,388,356,359,355,339,340,394,350,344,358,395,345,354,392,349,343,368,362,363,351,391,353,338,337M 0
417 351,353,338,337M,341,342,357,352,356,355,339,340,350,344,358,345,354,349,343 0
418 363,350,344,358,345,354,375,349,343,377,351,376,371,353,338,337M,341,372,342,369,357,352,378,374,370,356,373,355,339,340 0
419 368,362,355,391,339,340,361,363,390,350,344,396,358,345,354,349,343,360,393,351,388,353,338,337M,341,342,357,352,359,389,394,356,395,392 0
420 356,355,339,340,350,344,358,345,354,349,343,351,353,338,337M,341,342,357,352 0
421 395,392,368,362,391,356,361,363,390,355,396,339,340,360,393,350,344,358,388,345,354,349,343,351,353,338,337M,341,359,342,357,352,394 0
422 342,357,352,356,355,339,340,350,344,358,345,354,349,343,351,353,338,337M,341 0
424 347,353,338,337M,341,342,357,352,356,355,339,340,350,344,358,345,354,349,343,351 0
425 358,345,354,349,343,351,347,353,338,337M,341,342,357,352,356,355,339,340,350,344 0
426 339,340,385,382,350,344,358,345,354,381,349,343,375,351,377,376,353,338,337M,341,371,383,342,372,357,352,378,374,386,379,384,356,370,373,355 0
427 356,370,373,355,339,340,385,382,350,344,347,358,345,354,381,349,343,375,351,377,376,353,338,337M,380,341,371,369,383,342,372,357,352,378,374,386,379,384 0
428 394,356,395,355,392,368,362,339,340,391,350,344,361,363,390,347,358,345,354,396,349,343,351,360,393,353,338,337M,388,341,342,357,352,359 0
429 357,352,356,355,339,340,350,344,358,345,354,349,343,351,353,338,337M,341,342 0
430 353,338,337M,341,342,357,352,356,355,339,340,350,344,358,345,354,349,343,351 0
431 349,343,351,359,353,338,337M,341,342,394,357,352,395,392,368,362,391,361,363,390,356,396,355,360,393,339,388,350,344,358,345,354 0
432 350,344,358,345,354,349,343,351,353,338,337M,341,342,357,352,347,356,340,355,339 0
434 355,386,379,384,339,350,344,358,345,359,354,349,343,351,385,382,353,338,337M,341,381,342,368,362,357,352,380,361,383,363,347,360,356,340 0
439 378,374,360,340,370,355,386,373,379,384,339,350,344,358,345,359,354,349,343,351,385,375,382,377,353,338,337M,341,376,381,342,371,369,368,362,357,372,352,361,383,363 0
441 357,372,352,361,383,363,356,360,340,370,355,384,339,350,344,358,345,359,354,349,343,351,382,353,338,337M,341,381,380,342,371,369,368,362,347 0
1465 381,380,388,342,371,369,368,362,347,357,372,352,378,374,361M,383,363,356,386,360,373,379,394,340,370,355,395,384,392,339,391,350,344,385,358,375,345,359,354,377,390,349,343,376,396,351,382,353,393,338,337,341 0
1529 376,396,351,353,393,338,337,341,381,380,388,342,371,369,368,362,347,357,372,352,378,374,361,363,356,386,360,373,379,394,340,370,355,395,384,392,339,391,350,344,385,358,375,345,359,354,377,390,349M,343 0
1927 391,350,358,345,359,377,390,349,351,353,338,337,369,362,378,374,361M,363,379,340,370,339 0
1928 339,391,350,345,359,390,349,381,338,369,362,378,374,361M,395,379,370 0
1929 379,339,377,391,350,388,345,359,390,337,349,381,352,338,373,369,363M 0
1930 395,375,354,376,379,341,351,339,377,353,391,350,388,345,359,390,337,349,356,381,352,362,338,355,369,361M,363,340 0
1931 369,361M,385,363,340,396,395,376,380,342,371,379,372,339,353,391,350,388,345,360,359,390,337,349,381,374,362,338 0
1932 362M,338,369,379,377,339,394,391,352,350,378,388,345,359,390,349,384,381 0
1933 345,359,390,349,381,370,361M,362,363,338,340,396,369,341,376,372,379,386,339,360,337,394,391,352,350,374,388,355 0
1934 344,339,360,337,384,394,391,352,350,374,355,345,359,390,349,358,354,381,370,361M,362,363,395,338,340,396,369,342,377,356,372,379,373 0
1935 379,388,344,339,360,337,384,394,391,393,352,350,374,345,359,390,349,381,380,361M,362,363,395,338,340,376,369,342,372 0
1936 373,379,375,388,339,360,337,384,394,391,352,350,374,370,345,359,390,349,381,341,396,361M,362,363,395,338,353,376,369,386,377,356,378 0
1937 386,377,356,382,372,343,378,373,385,355,379,344,358,375,354,368,393,347,388,357,351,339,360,337,371,384,394,391,352,350,374,370,345,359,380,390,349,340,381,341,396,361M,362,363,395,342,338,353,392,376,369 0