Advertisement
_DarkLex_

Untitled

Sep 18th, 2023 (edited)
1,294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 0.82 KB | None | 0 0
  1. CREATE TABLE Company (
  2.     Id INT PRIMARY KEY,
  3.     Name TEXT
  4. );
  5.  
  6. INSERT INTO Company
  7. VALUES
  8.     (1, 'Mindbox'),
  9.     (2, 'Ozon'),
  10.     (3, 'Bosch'),
  11.     (4, 'Tesla');
  12.  
  13. CREATE TABLE Activity (
  14.     Id INT PRIMARY KEY,
  15.     Name TEXT
  16. );
  17.  
  18. INSERT INTO Activity
  19. VALUES
  20.     (1, 'IT'),
  21.     (2, 'Market'),
  22.     (3, 'Car'),
  23.     (4, 'Building');
  24.  
  25. CREATE TABLE CompanyActivity (
  26.     CompanyId INT,
  27.     ActivityId INT,
  28.     PRIMARY KEY (CompanyId, ActivityId),
  29.     FOREIGN KEY (CompanyId) REFERENCES Company(Id),
  30.     FOREIGN KEY (ActivityId) REFERENCES Activity(Id)
  31. );
  32.  
  33. INSERT INTO CompanyActivity
  34. VALUES
  35.     (1, 1),
  36.     (2, 1),
  37.     (2, 2),
  38.     (4, 3);
  39.  
  40. SELECT
  41.     Company.Name AS CompanyName,
  42.     Activity.Name AS ActivityName
  43. FROM Company
  44. LEFT JOIN CompanyActivity
  45.     ON Company.Id = CompanyActivity.CompanyId
  46. LEFT JOIN Activity
  47.     ON CompanyActivity.ActivityId = Activity.Id;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement