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 = 337 
WHERE 
  cscart_products_categories.product_id IN (
    421, 427, 408, 1971, 382, 418, 1959, 1930, 
    400, 1960, 424, 1961, 1931, 381, 1529, 
    401, 1465, 1962, 1933, 1935, 1958, 428, 
    415, 391, 406, 439, 390, 1969, 393, 394, 
    1929, 396, 397, 398, 399, 422, 1964, 
    434, 383, 412, 1927, 419, 430, 1965, 
    1966, 431, 432, 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, 441, 1957
  ) 
GROUP BY 
  cscart_products_categories.product_id

Query time 0.01644

JSON explain

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "241.36"
    },
    "grouping_operation": {
      "using_temporary_table": true,
      "using_filesort": true,
      "cost_info": {
        "sort_cost": "46.79"
      },
      "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.86",
            "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,1962,1933,1935,1958,428,415,391,406,439,390,1969,393,394,1929,396,397,398,399,422,1964,434,383,412,1927,419,430,1965,1966,431,432,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,441,1957))",
            "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.79",
              "eval_cost": "9.36",
              "prefix_cost": "194.57",
              "data_read_per_join": "748"
            },
            "used_columns": [
              "product_id",
              "category_id",
              "position"
            ]
          }
        }
      ]
    }
  }
}

Result

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