Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Не понятна изначальная организация хранения данных в базе:
- use Test1
- GO
- CREATE TABLE Products (
- Id INT PRIMARY KEY IDENTITY,
- Product NVARCHAR(20) NOT NULL,
- CategoryId INT
- )
- CREATE TABLE Categories (
- Id INT PRIMARY KEY IDENTITY,
- Category NVARCHAR(20) NOT NULL,
- )
- GO
- INSERT INTO Categories (Category)
- VALUES ('Gadgets'),
- ('Drinks'),
- ('Medicines'),
- ('Food'),
- ('Smartphones')
- GO
- INSERT Into Products (Product, CategoryId)
- VALUES ('Galaxy S21', 1),
- ('Coca Cola', 2),
- ('Nokia 3310', 1),
- ('Huawie FreeBuds 5i', 1),
- ('Парацетамол', 3),
- ('Milk 1L', 4),
- ('Galaxy S21', 5),
- ('XBox One X', NULL)
- -- Запрос на выборку
- SELECT p.Product as product, c.Category as category
- FROM Products AS p
- LEFT JOIN Categories AS c ON c.Id = p.CategoryId
- GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement