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 = 340 
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, 1729, 406, 439, 390, 1969, 
    393, 394, 396, 397, 398, 399, 422, 1964, 
    434, 383, 412, 1927, 419, 430, 324, 1965, 
    1966, 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, 387, 386, 416, 
    441, 1957
  ) 
GROUP BY 
  cscart_products_categories.product_id

Query time 0.01525

JSON explain

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "238.67"
    },
    "grouping_operation": {
      "using_temporary_table": true,
      "using_filesort": true,
      "cost_info": {
        "sort_cost": "45.57"
      },
      "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": 45,
            "filtered": "49.53",
            "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,1729,406,439,390,1969,393,394,396,397,398,399,422,1964,434,383,412,1927,419,430,324,1965,1966,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,387,386,416,441,1957))",
            "cost_info": {
              "read_cost": "92.00",
              "eval_cost": "9.11",
              "prefix_cost": "138.42",
              "data_read_per_join": "729"
            },
            "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": 45,
            "filtered": "100.00",
            "cost_info": {
              "read_cost": "45.57",
              "eval_cost": "9.11",
              "prefix_cost": "193.10",
              "data_read_per_join": "729"
            },
            "used_columns": [
              "product_id",
              "category_id",
              "position"
            ]
          }
        }
      ]
    }
  }
}

Result

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