Как выбрать список номеров, содержащий все элементы из списка?

Похоже, вам нужен способ представления различных объектов, так что идентичные пары ключ-значение (в разных порядках) могут быть распознаны как один и тот же ключ в map. Один из вариантов - взять объект и sort его записи в соответствии с алфавитным порядком клавиш, а затем stringify отсортированные записи:

var m = new Map();

function opening(config) {
  // ...
  return "foo";
}

function open(config = {}) {
  const key = objToKeyValStr(config);
  if (!m.has(key)) {
    m.set(key, opening(config));
    console.log("open and set: ", config);
  } else {
    console.log("get from the map: ", config);
  }

  return m.get(config);
}
function replacer(_, value) {
  if (typeof value !== 'object' || Array.isArray(value)) return value;
  return objToKeyValStr(value);
}
const objToKeyValStr = obj => (
  JSON.stringify(
    Object.entries(obj).sort((a, b) => a[0].localeCompare(b[0])),
    replacer
  )
);

var c1 = { a: 1, b: 2 };

open(c1); // open and set [OK]
open(c1); // get from the map [OK] 
open({ a: 1, b: 2 }); // get from the map
open({ b: 2, a: 1 }); // get from the map, even in different orders
open({ a: 1, b: 2, c: 3 }); // open and set
open({ a: 1, c: 3, b: 2 }); // get from the map

open({ a: 1, b: { c: 1, d: 1 }}); // open and set
open({ a: 1, b: { d: 1, c: 1 }}); // get from the map

-1
задан Barbaros Özhan 19 January 2019 в 20:38
поделиться

2 ответа

Если у вас есть список, вы можете сделать:

select room_id
from items_in_room
where item_id in ( . . . )  -- list of items here
group by room_id
having count(*) = <n>;  -- number of items in list

Это предполагает, что items_in_room не имеет дубликатов. Если это так, используйте count(distinct) для предложения having:

having count(distinct item_id) = <n>
0
ответ дан Gordon Linoff 19 January 2019 в 20:38
поделиться

Просто используйте оператор in как

select room_id 
  from items_in_room
 where item_id in (x,y,z)
0
ответ дан Barbaros Özhan 19 January 2019 в 20:38
поделиться
Другие вопросы по тегам:

Похожие вопросы: