Answer the question
In order to leave comments, you need to log in
How to match client ids randomly?
Good day to all! I have a database with clients who have their own id, I need to match these clients so that with the value "search" they can find a pair for themselves and at the same time exclude cases that two clients simultaneously receive the id of the same person.
Answer the question
In order to leave comments, you need to log in
It can be done, for example, as follows.
Create a table for clients and populate it with data:
CREATE TABLE clients (id INTEGER NOT NULL);
INSERT INTO clients (id) VALUES (1), (2), (3), (4), (5);
CREATE TABLE clients_groups (
first_client_id INTEGER NOT NULL UNIQUE,
second_client_id INTEGER NOT NULL UNIQUE,
FOREIGN KEY (first_client_id) REFERENCES clients (id) ON DELETE CASCADE ON UPDATE NO ACTION,
FOREIGN KEY (second_client_id) REFERENCES clients (id) ON DELETE CASCADE ON UPDATE NO ACTION,
CHECK(first_client_id != second_client_id)
);
SELECT id FROM clients c
WHERE
c.id NOT IN(SELECT first_client_id FROM clients_groups) AND
c.id NOT IN(SELECT second_client_id FROM clients_groups)
ORDER BY RANDOM()
LIMIT 1;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question