SQL-Statement for counting and calculating relational data - sql

I have a main-table "Restaurants" and an one-to-many relation to a "RestaurantVotes" table. One Restaurant can have multiple Votes. It's defined like this:
CREATE TABLE `restaurant_votes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`created_at` datetime NOT NULL,
`user_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`cat_food` int(11) NOT NULL,
`cat_cart` int(11) NOT NULL,
`cat_ambassador` int(11) NOT NULL,
`cat_drinks` int(11) NOT NULL,
`cat_service` int(11) NOT NULL,
`cat_ambience` int(11) NOT NULL,
`cat_price` int(11) NOT NULL,
`restaurant_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `IDX_1B96C91EB1E7706E` (`restaurant_id`),
CONSTRAINT `FK_1B96C91EB1E7706E` FOREIGN KEY (`restaurant_id`) REFERENCES `restaurants` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `restaurants` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
For an API-call we need to get all restaurants including name and the overall rating for this restaurant as a JSON response. Name etc. is no the problem, but we have big problems to find a SQL-query which gets the rating for each restaurant. The rating is calculated like this:
Summerize the values (integer 1-5) of all categories (cat_*) and divide it by the number of the categories (7).
Summerize the result of the ratings above and divide the value by the number of ratings in the restaurant_votes table per restaurant.
Return the overall rating from 2. in the SQL-query together with the name of the restaurant.
Is this even possible using SQL? Any advice? Thanks!

select
r.name,
sum(rv.cat_food +
rv.cat_cart +
rv.cat_ambassador +
rv.cat_drinks +
rv.cat_service +
rv.cat_ambience +
rv.cat_price) / 7 as total_score,
sum(rv.cat_food +
rv.cat_cart +
rv.cat_ambassador +
rv.cat_drinks +
rv.cat_service +
rv.cat_ambience +
rv.cat_price) / 7 / count(rv.id) as average_score
from restaurants r
left join restaurant_votes rv
on r.id = rv.restaurant_id
group by r.name
I've posted a SQLFiddle to demonstrate this.

SELECT r.name,r.id,score.rating
FROM
restaurants as r
INNER JOIN
(
SELECT restaurant_id,AVG(cat_food + cat_cart + cat_ambassador + cat_drinks + cat_service + cat_ambience + cat_price)/7 AS rating
FROM restaurant_votes
GROUP BY restaurant_id
)as score
ON r.id = score.rating
Would this work?
It gets all the restaurants defined in the restaurant table and and gets the average of all the columns added together and divided by 7.

Related

SQL find all records from one table, but only the most recent record from a linked table

I am looking to select all records from my userrecords table and then find the corresponding most recent record from my checkins table.
I need to do this so that I can display whether the user is checked-in or checked-out for the current day and campus.
The outdatetime is a NULL value when the user is currently checked-in, and the query should take into account the current date so that only checkin records for the current date are considered.
My table setup is like so:
CREATE TABLE `userrecords` (
`userid` int(11) NOT NULL AUTO_INCREMENT,
`firstname` varchar(45) NOT NULL,
`surname` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
`phone` varchar(15) DEFAULT NULL,
`password` char(64) DEFAULT NULL,
`userlevel` int(1) NOT NULL,
`suspended` varchar(1) DEFAULT NULL,
`created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`lastcheckdate` datetime DEFAULT NULL,
`maincampus` varchar(3) DEFAULT NULL,
`lastlogin` datetime DEFAULT NULL,
`staffid` varchar(6) DEFAULT NULL,
PRIMARY KEY (`userid`),
UNIQUE KEY `email_UNIQUE` (`email`),
UNIQUE KEY `userid_UNIQUE` (`userid`)
) ENGINE=InnoDB AU
CREATE TABLE `checkins` (
`recordid` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(11) NOT NULL,
`campusid` int(11) NOT NULL,
`indatetime` datetime DEFAULT NULL,
`outdatetime` datetime DEFAULT NULL,
PRIMARY KEY (`recordid`),
KEY `campusid_idx` (`campusid`),
KEY `userid_idx` (`userid`),
CONSTRAINT `campusid` FOREIGN KEY (`campusid`) REFERENCES `campus` (`campusid`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `userid` FOREIGN KEY (`userid`) REFERENCES `userrecords` (`userid`) ON DELETE CASCADE ON UPDATE CASCADE
My query is so far:
SELECT userid,firstname,surname,email,lastcheckdate FROM userrecords WHERE userlevel=0
You can use window functions:
select ur.*, c.*
from userrecords ur left join
(select c.*,
row_number() over (partition by userid order by indatetime desc) as seqnum
from checkins c
) c
on ur.userid = c.userid and ur.seqnum = 1;

How to use subqueries in MySQL

I have two tables.
Table user:
CREATE TABLE IF NOT EXISTS `user` (
`user_id` int(20) NOT NULL AUTO_INCREMENT,
`ud_id` varchar(50) NOT NULL,
`name` text NOT NULL,
`password` text NOT NULL,
`email` varchar(200) NOT NULL,
`image` varchar(150) NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB
and mycatch:
CREATE TABLE IF NOT EXISTS `mycatch` (
`catch_id` int(11) NOT NULL AUTO_INCREMENT,
`catch_name` text NOT NULL,
`catch_details` text NOT NULL,
`longitude` float(10,6) NOT NULL,
`latitude` float(10,6) NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`image` varchar(150) NOT NULL,
`user_id` int(20) NOT NULL,
PRIMARY KEY (`catch_id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB;
ALTER TABLE `mycatch`
ADD CONSTRAINT `mycatch_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE;
My goal is: I want to retrieve longitude and latitude from mycatch against given ud_id (from user) and catch_id (from mycatch) where ud_id = given ud_id and catch_id > given catch_id.
I used the query but fail to retrieve
SELECT ud_id=$ud_id FROM user
WHERE user_id =
(
SELECT user_id,longitude,latitude from mycatch
WHERE catch_id>'$catch_id'
)
The error is:
#1241 - Operand should contain 1 column(s)
First, try not to use subqueries at all, they're very slow in MySQL.
Second, a subquery wouldn't even help here. This is a regular join (no, Mr Singh, not an inner join):
SELECT ud_id FROM user, mycatch
WHERE catch_id>'$catch_id'
AND user.user_id = mycatch.user_id
Select m.longitude,m.latitude from user u left join mycatch m
on u.user_id =m.user_id
where u.ud_id=$ud_id and
m.catch_id >$catch_id

sql query is returning the same values twice

I have this sql query, and it should be returning two values, which is does but it returns each returned row twice, the sql looks like this,
SELECT * FROM `mailers`
LEFT JOIN `mailer_content` ON `mailers`.`id` = `mailer_content`.`mailer_id`
LEFT JOIN `mailer_images` ON `mailer_content`.`id` = `mailer_images`.`content_id`
WHERE `mailers`.`id` = 26
The table structure for the tables I am query look like this,
-- --------------------------------------------------------
--
-- Table structure for table `mailers`
--
CREATE TABLE `mailers` (
`id` int(11) NOT NULL auto_increment,
`mailer_title` varchar(150) NOT NULL,
`mailer_header` varchar(60) NOT NULL,
`mailer_type` enum('single','multi') NOT NULL,
`introduction` varchar(80) NOT NULL,
`status` enum('live','dead','draft') NOT NULL,
`flag` enum('sent','unsent') NOT NULL,
`date_mailer_created` int(11) NOT NULL,
`date_mailer_updated` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=29 ;
-- --------------------------------------------------------
--
-- Table structure for table `mailer_content`
--
CREATE TABLE `mailer_content` (
`id` int(11) NOT NULL auto_increment,
`headline` varchar(320) NOT NULL,
`content` text NOT NULL,
`mailer_id` int(11) NOT NULL,
`position` enum('left','right','centre') default NULL,
`tab_1_name` varchar(25) default NULL,
`tab_1_link` varchar(250) default NULL,
`tab_2_name` varchar(25) default NULL,
`tab_2_link` varchar(250) default NULL,
`tab_3_name` varchar(25) default NULL,
`tab_3_link` varchar(250) default NULL,
`tab_4_name` varchar(25) default NULL,
`tab_4_link` varchar(250) default NULL,
`created_at` int(10) NOT NULL,
`updated_at` int(10) NOT NULL,
PRIMARY KEY (`id`),
KEY `mailer_id` (`mailer_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ;
-- --------------------------------------------------------
--
-- Table structure for table `mailer_images`
--
CREATE TABLE `mailer_images` (
`id` int(11) NOT NULL auto_increment,
`title` varchar(150) NOT NULL,
`filename` varchar(150) NOT NULL,
`mailer_id` int(11) NOT NULL,
`content_id` int(11) default NULL,
`date_created` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=49 ;
I am sure that is must be a problem with my sql I just do not know what the problem is
If you use SELECT DISTINCT SQL will not return dupplicated rows, if there are some.
SELECT DISTINCT * FROM `mailers` LEFT JOIN `mailer_content` ON `mailers`.`id` = `mailer_content`.`mailer_id` LEFT JOIN `mailer_images` ON `mailer_content`.`id` = `mailer_images`.`content_id` WHERE `mailers`.`id` = 26
U can use group by smthng. It will delete the same records.
but u can delete nonsame rows. Use smthng without same values in different rows in original table.
try this
SELECT * FROM mailers
LEFT JOIN mailer_content ON mailers.id = mailer_content.mailer_id
LEFT JOIN mailer_images ON mailer_content.id = mailer_images.content_id
WHERE mailers.id = 26 GROUP BY mailers.id
It doesn't look like an SQL isse to me; I suspect this is more likely down to the data in your tables.
My guess is that there are two rows in mailer_content where mailers.id = 26 and then two rows (or possibly 1 and 3) in mailer_images for each of the mailer_contents.
How many rows do each of the following queries return?
SELECT * FROM `mailers`
WHERE `mailers`.`id` = 26
SELECT * FROM `mailer_content`
WHERE `mailer_content`.`id` = 26
My guess is that the first returns 1 row (because it has a primary key on id) and that the second returns two rows.
That all may be fine but my guess is that the following query returns 4 records:
SELECT * FROM `mailer_content`
LEFT JOIN `mailer_images` ON `mailer_content`.`id` = `mailer_images`.`content_id`
WHERE `mailer_content`.`id` = 26
Because either each content has two images each OR one content has one image and the other has three.

MySQL multi-level parent selection/join question

I have a hopefully simple MySQL query question which is eluding me at late at night. I'm trying to do a SELECT which counts the number of instances of a set of data (orders) and groups those instances by a value which exists in a parent a couple levels above the order itself.
For example:
CREATE TABLE `so_test`.`categories` (
`id` int(10) unsigned NOT NULL auto_increment,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=572395 DEFAULT CHARSET=latin1;
CREATE TABLE `so_test`.`product_group` (
`id` int(10) unsigned NOT NULL auto_increment,
`category_id` int(10) unsigned NOT NULL auto_increment,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=572395 DEFAULT CHARSET=latin1;
CREATE TABLE `so_test`.`products` (
`id` int(10) unsigned NOT NULL auto_increment,
`product_group_id` int(10) unsigned NOT NULL auto_increment,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=572395 DEFAULT CHARSET=latin1;
CREATE TABLE `so_test`.`orders` (
`id` int(10) unsigned NOT NULL auto_increment,
`product_id` int(10) unsigned NOT NULL auto_increment,
`customer_id` int(10) unsigned NOT NULL auto_increment,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=572395 DEFAULT CHARSET=latin1;
What I'm looking to do is something in the neighborhood of:
SELECT count(orders.id), categoryId
FROM orders, categories WHERE
orders.customer_id in (1,2,3) GROUP BY orders.productId.productGroupId.categoryId
Assuming there are 17 orders for products in category 1, 2 orders for products in category 2, and 214 orders for category 3, what I'm hoping to get back is:
count(orders.id), categoryId
============================
17 1
2 2
214 3
If I was trying to group by say product_id I'd be fine..but the two-levels-up portion is throwing me.
Thanks!
Just join them together:
select categoryid, count(orders.id)
from category c
left join product_group pg on pg.category_id = c.id
left join products on p on p.product_group_id = pg.id
left join orders o on o.product_id = p.id
For categories without an order, count(orders.id) will return 0, while count(*) would return one or more, depending on the number of productgroups and products.
An inner join would not count categories without orders at all.

SQL: Subquery assistance

I'm trying to SELECT two values in one statement instead of two statements. The first statement counts how many entries won for a specific contest that has an id of 18. The second statement counts the total quantity of prizes for that contest.
Query 1:
SELECT
COUNT(*) FROM entries
WHERE entries.contest=18
AND entries.won=1
Query 2
SELECT SUM( quantity ) FROM prizes WHERE contest=18
I want to have both so I could compare them on the server-side and act upon that comparison.. So if we had say 3 entrants who won the 18th contest and the total quantity of prizes for the 18th contest is 5, do something...
Schema:
CREATE TABLE `entries` (
`id` int(2) unsigned NOT NULL auto_increment,
`message_id` bigint(8) unsigned default NULL,
`user` int(2) unsigned default NULL,
`username` varchar(50) NOT NULL,
`contest` int(2) unsigned default NULL,
`message` text,
`icon` text,
`twitter_url` text,
`follower` int(2) unsigned default NULL,
`entry_date` timestamp NOT NULL default '0000-00-00 00:00:00',
`won` int(1) default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
CREATE TABLE `prizes` (
`id` int(2) unsigned NOT NULL auto_increment,
`name` varchar(25) NOT NULL,
`contest` int(2) NOT NULL,
`quantity` int(2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
There is no users table because I control the entries, and I'm pretty sure I won't get any duplicates so that's why the user name, etc is stored for the entry row.
As the queries doesn't have anything in common at all, you would use two subqueries to get them in the same result:
select
(select count(*) from entries where contest = 18 and won = 1) as wins,
(select sum(quantity) from prizes where contest = 18) as quantity