Advertisement
QwarkDev

Mindbox / task2

Nov 18th, 2021 (edited)
1,148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.70 KB | None | 0 0
  1.  
  2. /*
  3. drop table [Product.Category]
  4. drop table Product
  5. drop table Category
  6. */
  7.  
  8. create table Product
  9. (
  10.     ProductId       int identity primary key,
  11.     ProductName     nvarchar(50),
  12.     ProductPrice    money
  13. )
  14.  
  15. create table Category
  16. (
  17.     CategoryId      int identity primary key,
  18.     CategoryName    nvarchar(50)
  19. )
  20.  
  21. create table [Product.Category]
  22. (
  23.     PcID            int identity primary key,
  24.     ProductId       int,
  25.     CategoryId      int,
  26.     foreign key (ProductId)  references Product  (ProductId),
  27.     foreign key (CategoryId) references Category (CategoryId)
  28. )
  29.  
  30. go
  31.  
  32.  
  33. insert into Product (ProductName, ProductPrice) values
  34. ('Молоко', 100),
  35. ('Хлеб', 40),
  36. ('Пирожок (уставший)', 20)
  37.  
  38. insert into Category (CategoryName) values
  39. ('Жидкости'), ('Мучное'), ('По акции')
  40.  
  41. insert into [Product.Category] (ProductId, CategoryId) values
  42. (1, 1), (2, 2), (3, 2), (3, 3)
  43.  
  44. go
  45.  
  46.  
  47. -- непосредственная выборка:
  48.  
  49. select p.*, c.CategoryName from Product p
  50. left join [Product.Category] pc on pc.ProductId = p.ProductId
  51. left join Category c on c.CategoryId = pc.CategoryId
  52.  
  53. -- ну или же
  54.  
  55. select p.*, string_agg(c.CategoryName, ', ') as Categories from Product p
  56. left join [Product.Category] pc on pc.ProductId = p.ProductId
  57. left join Category c on c.CategoryId = pc.CategoryId
  58. group by p.ProductId, p.ProductName, p.ProductPrice
  59.  
  60.  
  61. /*
  62. Вывод:
  63.  
  64. ProductId   ProductName                                        ProductPrice          CategoryName
  65. ----------- -------------------------------------------------- --------------------- --------------------------------------------------
  66. 1           Молоко                                             100,00                Жидкости
  67. 2           Хлеб                                               40,00                 Мучное
  68. 3           Пирожок (уставший)                                 20,00                 Мучное
  69. 3           Пирожок (уставший)                                 20,00                 По акции
  70.  
  71. (4 rows affected)
  72.  
  73. ProductId   ProductName                                        ProductPrice          Categories
  74. ----------- -------------------------------------------------- --------------------- --------------------------------------------------
  75. 1           Молоко                                             100,00                Жидкости
  76. 2           Хлеб                                               40,00                 Мучное
  77. 3           Пирожок (уставший)                                 20,00                 Мучное, По акции
  78.  
  79. (3 rows affected)
  80.  
  81.  
  82. Completion time: 2021-11-18T15:36:12.2074363+03:00
  83.  
  84. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement