SQL: Outputting From Two Tables - mysql

Question Posed: List the people and the schools for people who attended schools in Missouri.
I have a table called Post and a table called People. Each person in people has a unitid that corresponds to a unitid in post, basically saying where they go to college at. Each college in Post has a unitid, and a state abbreviation field, stabbr.
I am able to use the following to get the formmated names, and UnitID from people but can't figure out how to add on the Universities Name as well. Universities names are instnm.
select concat_ws(" ",surname,givenname,middleInitial) as 'whole_name',
unitid
from people
where
unitid in (select unitid from post where stabbr like 'MO');
How Would I go about adding in the university names?
Output:
| Kittinger Rita G | 451316 |
| Cagle Marie P | 456302 |
| Martinez Shana C | 460996 |
| Price John O | 480338 |
| Kurtz Melvin L | 481580 |
| Olmstead Tabitha J | 481988 |
| Wynn Stephanie D | 481997 |
| Garrett Richard L | 482398 |
| Bell Diane R | 482398 |
+---------------------+--------+

Related

MySQL - Select Club Name and list all members in one query?

I have a "clubs" table and a "members" table
I'd like a query to return the club name (clubs.name) and each member's nickname (members.nickname)
I can get them all using an inner join,
SELECT clubs.name AS `Club Name`, members.nickname AS `Member Handle`
FROM clubs
INNER JOIN members ON clubs.club_id = members.club_id
but that shows the club.name field for each member.nickname.
| Club Name | Member Handle |
-------------------------------
| Club Yellow | Jim-Bob |
| Club Yellow | Clem |
| Club Yellow | Mustache Pete |
| Club Green | Ladyhawke |
| Club Green | Rosie |
Is it at all possible to receive result like so?
| Club Name | Member Handle |
-------------------------------
| Club Yellow | Jim-Bob |
| | Clem |
| | Mustache Pete |
| Club Green | Ladyhawke |
| | Rosie |
Thanks
No in mysql you can not generate the result like this, you need to use application level to do it. However you can format the data i.e. all the member-handle as comma-separated values
SELECT
clubs.name AS `Club Name`,
group_concat(members.nickname) AS `Member Handle`
FROM clubs
INNER JOIN members ON clubs.club_id = members.club_id
group by clubs.name

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.

MySQL Relational Division

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.

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=''