Advertisement
TheFaceTakt

Untitled

Apr 29th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.63 KB | None | 0 0
  1. CREATE TABLE Manufacturer
  2. (
  3.     manufacturerId INTEGER PRIMARY KEY,
  4.     city VARCHAR(255)
  5. );
  6.  
  7. CREATE TABLE Product
  8. (
  9.     productId INTEGER PRIMARY KEY,
  10.     weight FLOAT
  11. );
  12.  
  13. CREATE TABLE Shop
  14. (
  15.     shopId INTEGER PRIMARY KEY,
  16.     city VARCHAR(255)
  17. );
  18.  
  19. CREATE TABLE ManufacturerProduct
  20. (
  21.     manufacturerId INTEGER,
  22.     productId INTEGER
  23. );
  24.  
  25. CREATE TABLE ShopProduct
  26. (
  27.     shopId INTEGER,
  28.     productId INTEGER
  29. );
  30.  
  31. INSERT INTO Manufacturer (city)
  32. VALUES
  33. (
  34.        "Moscow"
  35. );
  36.  
  37. INSERT INTO Manufacturer (city)
  38. VALUES
  39. (
  40.        "Oslo"
  41. );
  42.  
  43. INSERT INTO Manufacturer (city)
  44. VALUES
  45. (
  46.        "Paris"
  47. );
  48.  
  49. INSERT INTO Shop (city)
  50. VALUES
  51. (
  52.        "Moscow"
  53. );
  54.  
  55. INSERT INTO Shop (city)
  56. VALUES
  57. (
  58.        "Oslo"
  59. );
  60.  
  61. INSERT INTO Shop (city)
  62. VALUES
  63. (
  64.        "Paris"
  65. );
  66.  
  67. INSERT INTO Product (weight)
  68. VALUES
  69. (
  70.     15.7
  71. );
  72.  
  73. INSERT INTO Product (weight)
  74. VALUES
  75. (
  76.     16.1
  77. );
  78.  
  79. INSERT INTO Product (weight)
  80. VALUES
  81. (
  82.     19.5
  83. );
  84.  
  85.  
  86. INSERT INTO ManufacturerProduct(manufacturerId, productId)
  87. VALUES
  88. (
  89.     3, 1
  90. );
  91.  
  92. INSERT INTO ShopProduct(shopId, productId)
  93. VALUES
  94. (
  95.     1, 1
  96. );
  97. SELECT * FROM Manufacturer;
  98. SELECT * FROM Product;
  99.  
  100.  
  101. /*
  102. SELECT ManufacturerProduct.manufacturerId FROM
  103.     ManufacturerProduct JOIN Product ON (ManufacturerProduct.productId = Product.productId)
  104. WHERE ManufacturerProduct.productId IN
  105.     (
  106.         SELECT productId FROM
  107.         (
  108.             SELECT productId, COUNT(DISTINCT City) AS M FROM
  109.                 ShopProduct JOIN Shop ON ShopProduct.shopId = Shop.shopId
  110.             GROUP BY productId
  111.         ) X
  112.        
  113.         WHERE X.M = (SELECT COUNT(DISTINCT City) from Shop)
  114.     );
  115.  
  116. SELECT ShopProduct.shopId FROM
  117.      (ShopProduct JOIN ManufacturerProduct ON (ShopProduct.productId = ManufacturerProduct.productId)) SM JOIN Manufacturer ON (SM.manufacturerId = Manufacturer.manufacturerId) JOIN Product ON (ShopProduct.productId = Product.productId)
  118. WHERE Product.weight > 15 AND (Manufacturer.city = "Berlin" OR Manufacturer.city = "Paris");*/
  119.  
  120. SELECT X.shopId FROM
  121.     (
  122.         SELECT ShopProduct.shopId, COUNT(DISTINCT ShopProduct.productId) AS Products FROM
  123.             ShopProduct
  124.         GROUP BY productId
  125.     ) X
  126. WHERE X.Products =
  127. (
  128.     SELECT COUNT(DISTINCT ShopProduct.productId) AS T FROM
  129.             ShopProduct
  130.         JOIN
  131.             (
  132.                 SELECT ShopProduct.productId FROM
  133.                     ShopProduct
  134.                 WHERE ShopProduct.shopId = 1
  135.             ) XX
  136.         ON
  137.             (ShopProduct.productId = XX.productId)
  138.         WHERE ShopProduct.shopId = X.shopId
  139.     GROUP BY ShopProduct.shopId
  140. )
  141.  
  142. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement