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

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 340,350,392,361,356,362,390,355,349,393,363,338M,357,352,359,388,396,395,360,351,354,368,391,353,358,394 0
381 353,358,371,339,340,373,383,350,341,375,372,361,356,370,385,384,380,374,379,343,362,344,345,337M,355,349,378,363,338,376,381,347,357,352,342,359,382,360,351,354,386 0
382 351,354,386,353,358,371,339,340,373,383,350,341,377,375,372,356,370,385,384,374,369,379,343,344,345,337M,355,349,378,338,376,381,347,357,352,342,382 0
383 338,376,381,347,360,357,352,342,351,354,386,353,358,371,339,361,340,373,350,341,377,375,372,356,370,362,385,384,374,343,363,344,345,337M,355,349,359,378,368 0
384 344,345,337M,355,349,338,347,357,352,342,351,354,353,358,339,340,350,341,356,343 0
385 343,359,378,394,368,344,376,345,337M,355,349,360,392,338,347,357,352,342,371,390,351,354,361,393,373,353,358,377,369,339,375,372,340,370,362,350,388,341,396,374,395,356,363,391 0
386 356,343,344,345,337M,355,349,338,347,357,352,342,351,354,353,358,339,340,350,341 0
387 339,340,350,341,356,343,344,345,337M,355,349,338,347,357,352,342,351,354,353,358 0
388 351,354,353,358,363,382,381,339,340,359,350,341,368,360,356,386,383,380,343,344,345,337M,355,349,361,379,338,385,384,347,357,352,342,362 0
389 347,357,352,342,374,351,354,378,353,358,382,376,381,339,340,350,341,356,386,383,371,380,343,344,373,345,337M,355,349,377,369,375,372,379,338,370,385,384 0
390 349,396,395,338,362,391,347,357,352,342,394,363,351,354,359,353,392,358,368,339,360,340,350,341,390,356,393,343,361,344,388,345,337M,355 0
391 343,344,345,337M,355,349,338,347,357,352,342,351,354,353,358,339,340,350,341,356 0
393 377,356,369,375,372,379,370,385,384,343,374,344,345,337M,355,349,338,378,382,347,357,376,352,342,381,351,354,353,358,386,383,371,339,380,340,350,341,373 0
394 339,380,340,350,341,373,377,356,369,375,372,379,370,385,384,343,374,344,345,337M,355,349,338,378,382,347,357,376,352,342,381,351,354,353,358,386,383,371 0
395 354,353,358,339,340,350,341,356,343,344,345,337M,355,349,338,347,357,352,342,351 0
396 371,351,354,353,358,373,339,377,340,369,350,341,375,372,370,356,374,343,344,378,345,337M,355,349,376,338,347,357,352,342 0
397 338,347,357,352,342,351,354,353,358,339,340,350,341,356,343,344,345,337M,355,349 0
398 345,344,337M,355,361,389,349,388,396,338,395,347,357,362,352,342,391,394,363,351,354,353,358,359,392,368,339,340,350,341,360,356,390,393,343 0
399 343,345,344,337M,355,349,338,347,357,352,342,351,354,353,358,339,340,350,341,356 0
400 341,356,343,345,344,337M,355,349,338,347,357,352,342,351,354,353,358,339,340,350 0
401 358,339,340,341,343,345,344,337M,349,338,352,342,351 0
402 390,351,393,350,358,361,339,388,340,356,341,396,395,362,391,355,394,343,363,345,344,337M,347,349,359,357,392,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,342,393,354,371,353,361,351,373,388,350,358,377,396,369,395,375,372,339,370,362,340,356,391,341,374,394,363,355,359,378,392,343,368,376,345,344 0
406 355,359,392,343,368,345,344,337M,360,347,349,357,338,390,352,342,393,354,353,361,351,388,350,358,396,395,339,362,340,356,391,341,394,363 0
407 394,363,386,383,355,380,359,392,343,368,345,344,337M,360,347,349,357,379,385,338,384,390,352,342,393,354,353,361,351,388,382,350,358,396,381,395,339,362,340,356,391,341 0
408 339,378,340,356,341,376,386,383,355,380,343,345,344,371,337M,347,349,357,379,373,385,338,384,377,369,352,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,360,351,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,360,351,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 395,350,358,391,339,340,356,361,341,394,362,392,355,343,345,344,338,363,337M,349,357,390,359,368,393,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,337M,388,349,357,396,395,352,389,391,342,354,361,353,394,351,350,358,362,392,339,340,356,341,390,359,368,363,393,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 368,363,393,360,355,388,343,396,395,345,344,338,337M,349,357,391,361,394,352,342,354,353,362,392,351,350,358,339,340,356,390,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,371,382,381,352,342,373,354,377,353,375,372,370,351,386,383,350,358,374,339,340,356,341,378,379,376,385,384,355,343,345,344,338,337M 0
427 343,345,344,338,337M,349,357,371,382,347,369,381,352,342,373,354,377,353,375,372,380,370,351,386,383,350,358,374,339,340,356,341,378,379,376,385,384,355 0
428 390,359,355,368,363,393,343,345,344,338,360,337M,349,357,388,396,347,395,352,342,354,391,353,361,351,394,350,358,362,392,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,359,351,368,363,350,358,382,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,359,369,351,368,363,373,350,358,382,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,359,369,351,368,363,356,350,358,382,360,381,339,372,380,340,370,341,347 0
1465 341,386,347,378,376,383,355,361M,390,343,379,393,345,344,338,385,337,349,357,362,388,384,352,396,342,395,354,373,353,371,391,377,359,369,351,375,368,363,356,350,394,358,382,360,374,381,339,372,380,340,370,392 0
1529 358,360,374,381,339,372,380,340,370,392,341,386,347,378,376,355,361,390,343,379,393,345,344,338,385,337,349M,357,362,388,384,352,396,342,395,354,373,353,371,391,377,359,369,351,375,368,363,356,350,394 0
1729 340M 0
1927 391,377,359,369,351,363,350,358,374,339,370,340,378,361M,390,379,345,338,337,349,362,353 0
1930 375,356,353,391,377,359,369,381,351,363,350,341,339,376,355,340,361M,390,379,345,338,388,337,349,352,362,395,354 0
1931 337,371,349,362,395,360,372,380,353,391,359,374,369,381,363,350,339,376,385,340,361M,390,396,342,379,345,338,388 0
1933 361M,352,390,396,379,394,345,338,388,337,349,362,386,360,372,370,341,391,359,374,369,381,363,350,355,339,376,340 0
1934 340,342,354,361M,352,390,396,395,379,394,345,338,356,337,358,349,377,362,360,372,370,391,344,359,374,369,381,363,350,355,384,339,373 0
1935 384,339,340,342,388,361M,352,390,395,379,380,394,345,338,337,349,362,393,360,372,391,344,359,374,369,381,363,350,376 0
1937 359,355,374,369,381,382,363,350,368,376,373,371,384,339,354,375,392,396,347,340,342,351,356,353,388,358,386,377,361M,352,390,395,379,380,394,345,338,343,378,341,337,349,362,357,370,393,360,385,372,391,344 0
1957 359,369,381,363,368,339,331,340,377,361M,352,379,338,337,349,362,370,360 0
1958 362,370,360,359,369,381,363,368,339,388,340,377,361M,352,379,338,337,349 0
1959 379,338,337,349,362,370,350,360,359,369,381,363,368,339,340,377,361M,352 0
1960 340,377,361M,352,379,338,337,349,362,370,360,359,369,381,363,368,378,339 0
1961 340,377,361M,379,338,350,337,349,362,370,360,359,369,381,363,368,378,339 0
1962 339,352,340,377,361M,379,338,350,337,349,362,390,370,360,388,359,369,381,363 0
1963 378,359,369,381,363,339,352,340,377,361M,379,338,350,337,349,362,370,360,388 0
1964 370,368,360,378,359,369,381,363,339,340,377,361M,379,338,337,349,362 0
1965 338,337,349,362,370,368,360,359,369,381,363,339,352,340,377,361M,379 0
1966 377,361M,379,338,337,349,362,370,368,360,359,369,381,363,339,352,340 0
1968 340,361M,379,338,337,349,362,370,360,359,369,381,363,350,339,352 0
1969 350,339,352,340,361M,379,338,337,368,349,362,370,360,359,369,381,363 0
1970 359,369,381,363,350,339,377,352,340,361M,379,338,337,368,349,362,370,360 0
1971 360,359,369,381,363,350,339,377,352,340,361M,379,338,337,368,349,362,370 0
1972 349,362,370,360,359,369,381,363,350,339,352,340,361M,379,338,337,368 0
1973 361M,379,338,337,368,349,362,370,360,359,369,381,377,363,339,352,340 0
1974 340,361M,379,338,337,368,349,362,370,360,350,359,369,381,363,339,352 0