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.01666

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