From ed04521b72a969de4d25564eff3f7b681473d3a4 Mon Sep 17 00:00:00 2001 From: Apostolof Date: Fri, 21 Dec 2018 18:12:46 +0200 Subject: [PATCH] Complete views --- Database implementation/queries/query_1.sql | 5 +- .../views/view_1_as_query.sql | 42 +++++++++-- .../views/view_2_as_query.sql | 74 +++++++++++++++++++ 3 files changed, 110 insertions(+), 11 deletions(-) create mode 100644 Database implementation/views/view_2_as_query.sql diff --git a/Database implementation/queries/query_1.sql b/Database implementation/queries/query_1.sql index cf6ac4d..574e4c1 100644 --- a/Database implementation/queries/query_1.sql +++ b/Database implementation/queries/query_1.sql @@ -1,13 +1,12 @@ SELECT * FROM ( - SELECT restaurant_id, + SELECT DISTINCT restaurant_id, restaurant_name, restaurant_category, restaurant_longitude, restaurant_latitude, restaurant_opening, restaurant_closing, - restaurant_is_approved, user_id, p.radius, p.distance_unit * DEGREES(ACOS(COS(RADIANS(p.latpoint)) @@ -15,7 +14,7 @@ FROM ( * COS(RADIANS(p.longpoint - z.restaurant_longitude)) + SIN(RADIANS(p.latpoint)) * SIN(RADIANS(z.restaurant_latitude)))) AS distance - FROM flavours_without_borders.restaurant AS z + FROM users_view AS z JOIN ( /* these are the query parameters */ SELECT 22.953012 AS latpoint, 40.635502 AS longpoint, 0.7 AS radius, 111.045 AS distance_unit ) AS p ON 1=1 diff --git a/Database implementation/views/view_1_as_query.sql b/Database implementation/views/view_1_as_query.sql index 06ce2e0..393eead 100644 --- a/Database implementation/views/view_1_as_query.sql +++ b/Database implementation/views/view_1_as_query.sql @@ -6,10 +6,33 @@ SELECT `restaurant`.`restaurant_id`, `restaurant`.`restaurant_opening`, `restaurant`.`restaurant_closing`, `restaurant`.`user_id`, - `food`.`food_id` AS `item_id`, - `food`.`food_name` AS `item_name`, - `food`.`food_description` AS `item_description`, - `food`.`food_calories` AS `item_calories`, + NULL AS `food_id`, + NULL AS `food_name`, + NULL AS `food_description`, + NULL AS `food_calories`, + NULL AS `drink_id`, + NULL AS `drink_name`, + NULL AS `drink_description`, + NULL AS `ingredient_name`, + NULL AS `ingredient_has_alcohol` +FROM flavours_without_borders.restaurant +WHERE restaurant.restaurant_is_approved = TRUE +UNION +SELECT `restaurant`.`restaurant_id`, + `restaurant`.`restaurant_name`, + `restaurant`.`restaurant_category`, + `restaurant`.`restaurant_longitude`, + `restaurant`.`restaurant_latitude`, + `restaurant`.`restaurant_opening`, + `restaurant`.`restaurant_closing`, + `restaurant`.`user_id`, + `food`.`food_id`, + `food`.`food_name`, + `food`.`food_description`, + `food`.`food_calories`, + NULL AS `drink_id`, + NULL AS `drink_name`, + NULL AS `drink_description`, `ingredient`.`ingredient_name`, `ingredient`.`ingredient_has_alcohol` FROM flavours_without_borders.restaurant @@ -29,10 +52,13 @@ SELECT `restaurant`.`restaurant_id`, `restaurant`.`restaurant_opening`, `restaurant`.`restaurant_closing`, `restaurant`.`user_id`, - `drink`.`drink_id` AS `item_id`, - `drink`.`drink_name` AS `item_name`, - `drink`.`drink_description` AS `item_description`, - NULL AS `item_calories`, + NULL AS `food_id`, + NULL AS `food_name`, + NULL AS `food_description`, + NULL AS `food_calories`, + `drink`.`drink_id`, + `drink`.`drink_name`, + `drink`.`drink_description`, `ingredient`.`ingredient_name`, `ingredient`.`ingredient_has_alcohol` FROM flavours_without_borders.restaurant diff --git a/Database implementation/views/view_2_as_query.sql b/Database implementation/views/view_2_as_query.sql new file mode 100644 index 0000000..8e8ed0c --- /dev/null +++ b/Database implementation/views/view_2_as_query.sql @@ -0,0 +1,74 @@ +SELECT + `restaurant`.`restaurant_id` AS `restaurant_id`, + `restaurant`.`restaurant_name` AS `restaurant_name`, + `restaurant`.`restaurant_category` AS `restaurant_category`, + `restaurant`.`restaurant_longitude` AS `restaurant_longitude`, + `restaurant`.`restaurant_latitude` AS `restaurant_latitude`, + `restaurant`.`restaurant_opening` AS `restaurant_opening`, + `restaurant`.`restaurant_closing` AS `restaurant_closing`, + `restaurant`.`user_id` AS `user_id`, + NULL AS `food_id`, + NULL AS `food_name`, + NULL AS `food_description`, + NULL AS `food_calories`, + NULL AS `drink_id`, + NULL AS `drink_name`, + NULL AS `drink_description`, + NULL AS `ingredient_name`, + NULL AS `ingredient_has_alcohol` +FROM `restaurant` +WHERE (`restaurant`.`restaurant_is_approved` = FALSE) +UNION +SELECT + `restaurant`.`restaurant_id` AS `restaurant_id`, + `restaurant`.`restaurant_name` AS `restaurant_name`, + `restaurant`.`restaurant_category` AS `restaurant_category`, + `restaurant`.`restaurant_longitude` AS `restaurant_longitude`, + `restaurant`.`restaurant_latitude` AS `restaurant_latitude`, + `restaurant`.`restaurant_opening` AS `restaurant_opening`, + `restaurant`.`restaurant_closing` AS `restaurant_closing`, + `restaurant`.`user_id` AS `user_id`, + `food`.`food_id` AS `food_id`, + `food`.`food_name` AS `food_name`, + `food`.`food_description` AS `food_description`, + `food`.`food_calories` AS `food_calories`, + NULL AS `drink_id`, + NULL AS `drink_name`, + NULL AS `drink_description`, + `ingredient`.`ingredient_name` AS `ingredient_name`, + `ingredient`.`ingredient_has_alcohol` AS `ingredient_has_alcohol` +FROM `restaurant` + LEFT JOIN `food` + ON `restaurant`.`restaurant_id` = `food`.`restaurant_id` + LEFT JOIN `food_has_ingredient` + ON `food`.`food_id` = `food_has_ingredient`.`food_id` + LEFT JOIN `ingredient` + ON `food_has_ingredient`.`ingredient_name` = `ingredient`.`ingredient_name` +WHERE `food`.`food_is_approved` = FALSE +UNION +SELECT + `restaurant`.`restaurant_id` AS `restaurant_id`, + `restaurant`.`restaurant_name` AS `restaurant_name`, + `restaurant`.`restaurant_category` AS `restaurant_category`, + `restaurant`.`restaurant_longitude` AS `restaurant_longitude`, + `restaurant`.`restaurant_latitude` AS `restaurant_latitude`, + `restaurant`.`restaurant_opening` AS `restaurant_opening`, + `restaurant`.`restaurant_closing` AS `restaurant_closing`, + `restaurant`.`user_id` AS `user_id`, + NULL AS `food_id`, + NULL AS `food_name`, + NULL AS `food_description`, + NULL AS `food_calories`, + `drink`.`drink_id` AS `drink_id`, + `drink`.`drink_name` AS `drink_name`, + `drink`.`drink_description` AS `drink_description`, + `ingredient`.`ingredient_name` AS `ingredient_name`, + `ingredient`.`ingredient_has_alcohol` AS `ingredient_has_alcohol` +FROM `restaurant` + LEFT JOIN `drink` + ON `restaurant`.`restaurant_id` = `drink`.`restaurant_id` + LEFT JOIN `drink_has_ingredient` + ON `drink`.`drink_id` = `drink_has_ingredient`.`drink_id` + LEFT JOIN `ingredient` + ON `drink_has_ingredient`.`ingredient_name` = `ingredient`.`ingredient_name` +WHERE `drink`.`drink_is_approved` = FALSE \ No newline at end of file