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 = 339 
WHERE 
  cscart_products_categories.product_id IN (
    421, 427, 408, 1971, 382, 418, 1959, 1930, 
    400, 1960, 424, 1961, 1931, 381, 1529, 
    401, 1465, 1932, 1962, 1933, 1935, 1958, 
    428, 415, 391, 406, 329, 439, 390, 1969, 
    393, 394, 1929, 396, 397, 398, 399, 422, 
    1964, 434, 383, 412, 1927, 419, 430, 
    1965, 1966, 431, 432, 1928, 1973, 429, 
    1968, 384, 1963, 1934, 395, 420, 385, 
    405, 410, 413, 1970, 417, 1937, 414, 
    425, 407, 389, 402, 1972, 388, 426, 409, 
    1974, 404, 1936, 387, 386, 416
  ) 
GROUP BY 
  cscart_products_categories.product_id

Query time 0.02886

JSON explain

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "241.40"
    },
    "grouping_operation": {
      "using_temporary_table": true,
      "using_filesort": true,
      "cost_info": {
        "sort_cost": "46.81"
      },
      "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": 46,
            "filtered": "50.88",
            "index_condition": "(`eshopuga_upgr_4122`.`cscart_products_categories`.`product_id` in (421,427,408,1971,382,418,1959,1930,400,1960,424,1961,1931,381,1529,401,1465,1932,1962,1933,1935,1958,428,415,391,406,329,439,390,1969,393,394,1929,396,397,398,399,422,1964,434,383,412,1927,419,430,1965,1966,431,432,1928,1973,429,1968,384,1963,1934,395,420,385,405,410,413,1970,417,1937,414,425,407,389,402,1972,388,426,409,1974,404,1936,387,386,416))",
            "cost_info": {
              "read_cost": "92.00",
              "eval_cost": "9.36",
              "prefix_cost": "138.42",
              "data_read_per_join": "748"
            },
            "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": 46,
            "filtered": "100.00",
            "cost_info": {
              "read_cost": "46.81",
              "eval_cost": "9.36",
              "prefix_cost": "194.60",
              "data_read_per_join": "748"
            },
            "used_columns": [
              "product_id",
              "category_id",
              "position"
            ]
          }
        }
      ]
    }
  }
}

Result

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