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 = 352 
WHERE 
  cscart_products_categories.product_id IN (
    421, 427, 408, 1971, 382, 418, 1959, 1930, 
    400, 1960, 424, 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, 434, 383, 
    412, 435, 419, 430, 324, 1965, 1966, 
    431, 432, 1973, 429, 1968, 384, 1963, 
    327, 1934, 395, 420, 385, 405, 410, 413, 
    1970, 417, 1937, 414, 425, 407, 323, 
    389, 402, 1972, 388, 426, 409, 1974, 
    404, 325, 1936, 387, 386, 416
  ) 
GROUP BY 
  cscart_products_categories.product_id

Query time 0.02143

JSON explain

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "241.79"
    },
    "grouping_operation": {
      "using_temporary_table": true,
      "using_filesort": true,
      "cost_info": {
        "sort_cost": "46.99"
      },
      "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": "51.07",
            "index_condition": "(`eshopuga_upgr_4122`.`cscart_products_categories`.`product_id` in (421,427,408,1971,382,418,1959,1930,400,1960,424,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,434,383,412,435,419,430,324,1965,1966,431,432,1973,429,1968,384,1963,327,1934,395,420,385,405,410,413,1970,417,1937,414,425,407,323,389,402,1972,388,426,409,1974,404,325,1936,387,386,416))",
            "cost_info": {
              "read_cost": "92.00",
              "eval_cost": "9.40",
              "prefix_cost": "138.42",
              "data_read_per_join": "751"
            },
            "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.99",
              "eval_cost": "9.40",
              "prefix_cost": "194.81",
              "data_read_per_join": "751"
            },
            "used_columns": [
              "product_id",
              "category_id",
              "position"
            ]
          }
        }
      ]
    }
  }
}

Result

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