Есть значит следующая БД со связью many to many:
create table products (product_id integer NOT NULL, title varchar(100), PRIMARY KEY (product_id));
create table categories (category_id integer NOT NULL, title varchar(100), PRIMARY KEY (category_id));
create table products_category
(
product_id integer NOT NULL,
category_id integer NOT NULL,
PRIMARY KEY (product_id, category_id),
FOREIGN KEY (product_id) REFERENCES products(product_id),
FOREIGN KEY (category_id) REFERENCES categories(category_id)
);
insert into categories (category_id, title) values (1, 'category1');
insert into categories (category_id, title) values (2, 'category2');
insert into categories (category_id, title) values (3, 'category3');
insert into categories (category_id, title) values (4, 'category4');
insert into products (product_id, title) values (1, 'product1');
insert into products (product_id, title) values (2, 'product2');
insert into products (product_id, title) values (3, 'product3');
insert into products (product_id, title) values (4, 'product4');
insert into products_category (product_id, category_id) values (1, 1);
insert into products_category (product_id, category_id) values (1, 2);
insert into products_category (product_id, category_id) values (1, 3);
insert into products_category (product_id, category_id) values (2, 3);
insert into products_category (product_id, category_id) values (2, 4);
insert into products_category (product_id, category_id) values (3, 1);
Составляю запрос:
select products.title, categories.title from products
inner join products_category on products_category.product_id = products.product_id
inner join categories on products_category.category_id = categories.category_id;
Вроде бы всё работает, однако не выводится product4, у которого нет ни одной категории, так вот, мне нужно, чтобы и он тоже выводился