MySQL Relational Division - mysql

I am having difficulties to solve one exercise:
For which People there is a Restaurant, that serves ALL their favorite beers.
(Yes, we actually have this in school :D)
I have got 2 Tables that can be used:
Table1: Favoritebeer (Name, Surname, beername)
Table2: OnStock (beername, restaurant, quantity)
My solution would be: OnStock % Favoritebeer
There is no such thing like DIVISION in MySQL. Any ideas how I could solve that? I found the following on Wikipedia: http://en.wikipedia.org/wiki/Relational_algebra#Division_.28.C3.B7.29 which is exactly what I need but I am having difficulties to translate it in SQL.
EDIT:
Here sample data: http://www.sqlfiddle.com/#!2/34e00
The result should be:
Bucher Rolf
Mastroyanni Pepe
Meier Hans
Meier Hanspeter
Meier Hansruedi
Müller Heinrich
Peters Peter
Zarro Darween

Give this a try:
SELECT DISTINCT fb1.name, fb1.surname FROM favoriteBeer fb1
JOIN stock s ON fb1.beerName = s.beerName
GROUP BY fb1.name, fb1.surname, s.restaurant
HAVING COUNT(*) = (
SELECT COUNT(*) FROM favoriteBeer fb2
WHERE fb1.name = fb2.name AND fb1.surname = fb2.surname
)
Output:
| NAME | SURNAME |
|-------------|-----------|
| Bucher | Rolf |
| Mastroyanni | Pepe |
| Meier | Hans |
| Meier | Hanspeter |
| Meier | Hansruedi |
| Müller | Heinrich |
| Peters | Peter |
| Zarro | Darween |
Fiddle here.

Related

SQL query that searches comma-delimited field

I have a student table which looks something like this:
id | name | school_descriptors
-------------------------------------------------------
1 | Rob | Comp Sci,Undergraduate,2020
2 | Tim | Business,MBA,2022
3 | Matt | Business,MBA,2022
4 | Jack | Law,Masters,2024
5 | Steph | Comp Sci,Masters,2022
The school_descriptors field is just one column, and stores information about the Course, Qualification and Graduation year as a comma-delimited string. (it's terribly designed and I wish it could be split up into its own fields, but it can't right now (I am not the database owner))
I want to provide an interface where teachers can quickly find students that match certain Course, Qualifications and Graduation years, and thus would like to create relevant queries.
Question 1: For example, I would like a teacher to be able to select from the UI: "Business", "MBA" and get returned students with ID 2 and 3. Specifically, an example question I have is: Find students who are in the Business Course and doing the MBA qualification:
SELECT * FROM student_table WHERE school_descriptors LIKE '%Business%' AND school_descriptors LIKE '%MBA%'
The query I have in mind is a basic LIKE query, but I can't help but think there is a more efficient query that can take advantage of the fact that the school_descriptor string is 1) always in a specific order (e.g. course, qualification, graduation), and 2) comma-delimited, and thus could be perhaps split. The table currently sits at ~5000 rows so relatively small but is expected to grow.
Related question 2: Find students who are in the Comp Sci Course and graduating after 2019:
Would it be possible to split the school_descriptors field and add a >2019 operand?
Many thanks!
In MySql you can use the function SUBSTRING_INDEX() to split the column school_descriptors.
This will work only if the positions of Course, Qualification and Graduation year are fixed.
select *,
substring_index(school_descriptors, ',', 1) Course,
substring_index(substring_index(school_descriptors, ',', 2), ',', -1) Qualification,
substring_index(school_descriptors, ',', -1) Graduation
from student_table
See the demo.
Results:
> id | name | school_descriptors | Course | Qualification | Graduation
> -: | :---- | :-------------------------- | :------- | :------------ | :---------
> 1 | Rob | Comp Sci,Undergraduate,2020 | Comp Sci | Undergraduate | 2020
> 2 | Tim | Business,MBA,2022 | Business | MBA | 2022
> 3 | Matt | Business,MBA,2022 | Business | MBA | 2022
> 4 | Jack | Law,Masters,2024 | Law | Masters | 2024
> 5 | Steph | Comp Sci,Masters,2022 | Comp Sci | Masters | 2022
select id, name,
substring_index(school_descriptors,',',1) as course,
substring_index(substring(school_descriptors,length(substring_index(school_descriptors,',',1))+2,200),',',1) as Qualifications,
substring_index(school_descriptors,',',-1) as year
from student;
output:
+------+-------+----------+----------------+------+
| id | name | course | Qualifications | year |
+------+-------+----------+----------------+------+
| 1 | Rob | Comp Sci | Undergraduate | 2020 |
| 2 | Tim | Business | MBA | 2022 |
| 3 | Matt | Business | MBA | 2022 |
| 4 | Jack | Law | Masters | 2024 |
| 5 | Steph | Comp Sci | Masters | 2022 |
+------+-------+----------+----------------+------+
A link to the docs, in case you want to know about SUBSTRING_INDEX()
Answer 1:
SELECT * FROM student_table WHERE school_descriptors REGEXP ['Business','MBA']
By using this query you can get all the records that are having Business OR MBA.
If you want to select only Business, MBA you can try like this
SELECT * FROM student_table WHERE school_descriptors LIKE '%Business,MBA%'
Answer 2:
SELECT *
FROM student
WHERE
SUBSTRING_INDEX(SUBSTRING_INDEX(school_descriptors , ',', 1), ',', -1)='Comp Sci'
AND
SUBSTRING_INDEX(SUBSTRING_INDEX(school_descriptors , ',', 3), ',', -1)> 2019;

SQL: how can I use GROUP BY to take an aggregate of an aggregate?

I have a query that groups by (column_a, column_b) and selects an aggregated value. I would like to then group by column_a and take an aggregate sum of the previously aggregated values.
Probably clearer with an example:
We have 3 tables: projects, devs, and contributors. Each project has many contributors, and each dev is a contributor to many projects:
+======== projects =========+ +====== devs =======+
+--------------+------------+ +--------+----------+
| project_name | project_id | | dev_id | dev_name |
+--------------+------------+ +--------+----------+
| parsalot | 1 | | 1 | Ally |
| vimplug | 2 | | 2 | Ben |
| gamify | 3 | | 3 | Chris |
+--------------+------------+ +--------+----------+
+==== contributors ===+
+------------+--------+
| project_id | dev_id |
+------------+--------+
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 2 |
| 3 | 3 |
+------------+--------+
I'm interested in how much work goes into each project. I could just count how many contributors each has, but I'd like to give more weight to contributions made by devs who aren't splitting their time over lots of other projects.
So vimplug is more actively developed than parsalot: each project has two contributors, but one of vimplug's (Ally) does nothing else, whereas parsalot's contributors are both splitting their time across other projects.
I've constructed a query that groups by (project, contributor) and calculates each contributors "dedication" to the project:
SELECT
projects.project_name,
devs.dev_name,
1 / COUNT(contributions.project_id) as dedication
FROM
projects
JOIN
contributors USING (project_id)
JOIN
devs USING (dev_id)
JOIN
contributors contributions USING (dev_id)
GROUP BY projects.project_id , contributors.dev_id;
Which yields,
+--------------+----------+------------+
| project_name | dev_name | dedication |
+--------------+----------+------------+
| parsalot | Ben | 0.5000 |
| parsalot | Chris | 0.5000 |
| vimplug | Ally | 1.0000 |
| vimplug | Ben | 0.5000 |
| gamify | Chris | 0.5000 |
+--------------+----------+------------+
What I really want, though, is the total dedication for each project, i.e.
+--------------+------------------+
| project_name | total_dedication |
+--------------+------------------+
| gamify | 0.5000 |
| parsalot | 1.0000 |
| vimplug | 1.5000 |
+--------------+------------------+
I (naively) tried changing my select statement to
SELECT
projects.project_name,
SUM(1 / COUNT(contributions.project_id)) as total_dedication
but that doesn't work ("Invalid use of group function"). Is there a way I can do this without having to do a sub-select?
Just use a subquery:
select project_name, sum(dedication)
from (<your query here>) q
group by project_name;
You are close to the solution please use the following :
SELECT project_name,sum(dedication) as total_dedication FROM (SELECT
projects.project_name,
devs.dev_name,
1 / COUNT(contributions.project_id) as dedication
FROM
projects
JOIN
contributors USING (project_id)
JOIN
devs USING (dev_id)
JOIN
contributors contributions USING (dev_id)
GROUP BY projects.project_id , contributors.dev_id) as A GROUP BY project_name
Ivan,
You asked "Is there a way I can do this without having to do a sub-select" ... is there a reason you cannot sub-select?
Unfortunately, you'll need to use a sub-select, because you cannot combine aggregate functions (which would be the only way you'd be able to accomplish this). See: How to combine aggregate functions in MySQL?
So as the other answers have shown, you'll have to use a sub-query.

Populate a column in MySQL with column value from another table

I am trying to do something in SQL that I imagined would be very basic, but I absolutely cannot seem to figure it out.
I have 2 tables :
Provinces ->
id | name | pCode | country_id | cCode
78840113-a0e5-11e4-8237-de7fe3f523cf | Alabama | AL | 1228 |
7884030c-a0e5-11e4-8237-de7fe3f523cf | Alaska | AK | 1228 |
788403ea-a0e5-11e4-8237-de7fe3f523cf | Arizona | AZ | 1228 |
788404a2-a0e5-11e4-8237-de7fe3f523cf | Arkansas | AR | 1228 |
and Countries ->
iso_code | name | country_id
AD | Andorra | 1005
AE | United Arab Emirates | 1225
AF | Afghanistan | 1001
AG | Antigua and Barbuda | 1009
I just want to have the cCode column in Provinces populated with the appropriate iso_code (if country_id in provinces and countries are the same).
I have tried so many things it isn't even writing my code here, I don't even know which direction is the correct way to go for this (join, insert, update??). I am completely stuck please help me!
If you are really using Mysql:
update Provinces p
inner join countries c on
p.country_id = c.country_id
set p.cCode = c.iso_code
You can use the UPDATE...JOIN syntax here:
UPDATE provinces JOIN countries USING (country_id)
SET provinces.cCode=countries.iso_code

How to condense a column like this?

I've tried finding something like this, but to no avail...
This is about a system of tables for a customer management system. In particular, I need to create a note history for each customer.
So, I have a table 'customers' with the columns customers.customer_ID, customers.lastname, customers.firstname, customers.postal_code, customers.city and customers.street;
and another table 'notes' with the columns notes.note_ID, notes.customer_ID, notes.subject, notes.description and notes.entered_on
Now I need to create a third table search which condenses much of the information above. It has the tables search.contact_ID, search.name, search.address and search.history. This is supposed to look like this:
contacts:
contact_ID | lastname | firstname | ...
------------+-----------+-----------+-----
1 | Doe | John | ...
2 | Dane | Jane | ...
note:
note_ID | contact_ID | subject | description | entered_on
--------+---------------+-----------------------+-----------------------+----------------
1 | 1 | call received | John Doe called us to | 2014-05-03
| | | ask for an offer |
2 | 1 | offer made | We called John Doe to | 2014-06-03
| | | submit our offer |
3 | 2 | advertisement call | We called Jane Dane to| 2014-06-03
| | | inform her of our |
| | | latest offer |
4 | 1 | offer accepted | John Doe called to | 2014-08-03
| | | accept our offer |
search:
contact_ID | name | address | history
------------+---------------+---------------------------------+-------------------
1 | Doe, John | 55 Main Street, 12345 Oldtown | 'On 2014-08-03 offer accepted: John Doe accepted our offer.
| | | On 2014-06-03 offer made: We called John Doe to submit our offer.
| | | On 2014-05-03 call received: John Doe called us to ask for an offer.'
2 | Dane, Jane | 111 Wall Street, 67890 Newtown | 'On 2014-06-03 advertisement call: We called Jane Dane to submit our offer.'
While I can deal with much of the rest, I have no idea how to generate the history information. My idea was as follows
WHILE
customers.customer_ID = note.customer_ID
AND
note.entered_on = GREATEST(note.entered_on)
DO
SET customers.note_history = CONCAT_WS(' | ', CONCAT_WS(': ',note.subject,note.description), customers.note_history);
But that one isn't necessarily chronological. Also how do I transform that into a statement compatible with the SELECT INTO used for the creation of the rest of the table?
Sounds like a case for a Group-By, along with GROUP_CONCAT
CREATE TABLE search (PRIMARY KEY(contact_ID))
SELECT contact_ID, CONCAT(lastname,', ',firstname) AS name, address,
GROUP_CONCAT(CONCAT('On ',entered_on,' ',subject,': ',description)
ORDER BY note_ID SEPARATOR "\n") AS history
FROM contacts LEFT JOIN note USING (contact_ID)
GROUP BY contact_ID
If dont want to use CREATE TABLE .. SELECT ... , can first just create (or truncate!) the table, and then use INSERT INTO ... SELECT ... instead.

SQL JOIN: Just not able to understand them

Now, I know know this question related to JOIN have been asked many times. I went through many of them. But it still isn't clear to me. I read these aricles too: http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins#_comments and http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html but no, it still didn't help.
I do understand the Vein diagrams mathematically but not able to grab basic concept behind JOIN.
Suppose I have two tables.
tbl_bookdescription:
| BOOKID | BOOKNAME | BOOKREVIEW | AUTHORID |
------------------------------------------------
| 1 | SQL Basics | Cool | 1 |
| 2 | PHP Basics | Good | 2 |
| 3 | AJAX Basics | Superb | 2 |
| 4 | HTML Basics | Very Good | 3 |
tbl_authordescription
| AUTHORID | AUTHORNAME |
-------------------------
| 1 | Tom |
| 2 | Jerry |
| 3 | Phil |
I want to script a search engine for my website
So, when the user enters Tom as $searchTerm, I want the program to return the name of the book which is written by Tom. And at the same time, the user can also enter Good. This time the query should again return the name of the book. So, I thought to do something like this
SELECT bookname FROM tbl_bookdescription MATCH(bookReview) AGAINST('$searchTerm')`
and then UNION this table with SOMETHING (something which matches authorName against $searchterm).
Now, two questions:
Is this query right? Will it give me the desired results?
WHat should I write in the code in place of SOMETHING. I think I will have to JOIN both the tables(not sure). And don't know how should I join.
Help appreciated.
If you search using only one search term then your query might look like
SELECT b.*, a.*
FROM tbl_bookdescription b JOIN tbl_authordescription a
ON b.authorID = a.authorID
WHERE b.bookName LIKE '%searchterm%'
OR b.bookReview LIKE '%searchterm%'
OR a.authorName LIKE '%searchterm%'
If you replace searchterm with 'Tom' you'll get
| BOOKID | BOOKNAME | BOOKREVIEW | AUTHORID | AUTHORNAME |
------------------------------------------------------------
| 1 | SQL Basics | Cool | 1 | Tom |
Now, if it's 'Good' then
| BOOKID | BOOKNAME | BOOKREVIEW | AUTHORID | AUTHORNAME |
-------------------------------------------------------------
| 2 | PHP Basics | Good | 2 | Jerry |
| 4 | HTML Basics | Very Good | 3 | Phil |
Here is SQLFiddle demo
Try this query
SELECT
a.*
FROM
tbl_bookdescription a
INNER JOIN
tbl_authordescription b
ON
a.authorid = b.authorid
WHERE
b.authorname=''