Advertisement
technologist02

SQL query Product version1

Dec 3rd, 2023
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.84 KB | Source Code | 0 0
  1. -- Не понятна изначальная организация хранения данных в базе:
  2.  
  3. use Test1
  4. GO
  5.  
  6. CREATE TABLE Products (
  7.     Id INT PRIMARY KEY IDENTITY,
  8.     Product NVARCHAR(20) NOT NULL,
  9.     CategoryId INT
  10. )
  11.  
  12. CREATE TABLE Categories (
  13.     Id INT PRIMARY KEY IDENTITY,
  14.     Category NVARCHAR(20) NOT NULL,
  15. )
  16. GO
  17.  
  18. INSERT INTO Categories (Category)
  19. VALUES ('Gadgets'),
  20. ('Drinks'),
  21. ('Medicines'),
  22. ('Food'),
  23. ('Smartphones')
  24. GO
  25.  
  26. INSERT Into Products (Product, CategoryId)
  27. VALUES ('Galaxy S21', 1),
  28. ('Coca Cola', 2),
  29. ('Nokia 3310', 1),
  30. ('Huawie FreeBuds 5i', 1),
  31. ('Парацетамол', 3),
  32. ('Milk 1L', 4),
  33. ('Galaxy S21', 5),
  34. ('XBox One X', NULL)
  35.  
  36. -- Запрос на выборку
  37. SELECT p.Product as product, c.Category as category
  38.   FROM Products AS p
  39.   LEFT JOIN Categories AS c ON c.Id = p.CategoryId
  40. GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement