Populate a column in MySQL with column value from another table - mysql

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

Related

How to order the rows in Laravel based on a column's value?

I have a table of products with city, state and country name.
If a user visits my shop, the products from his city should be displayed first, then the products from his state and finally the products from his country.
For a table,
+----+---------+-------------+------------+---------------+|
| id | product | city | state | country |
+----+---------+-------------+------------+---------------+
| 1 | guava | Julian | California | United States |
| 2 | apple | London | NA | England |
| 3 | orange | Los Angeles | California | United States |
| 4 | grapes | Zion | Illinois | United States |
| 5 | banana | Canyon | California | United States |
+----+---------+-------------+------------+---------------+
if a user from Los Angeles visits my shop, the expected results should be of order 3,1,5,3,2.
I read the laravel docs and also tried searching in stack overflow but couldn't find any apt answers.
My code for now is
Products::where('city",$usercity)->orWhere('state',$userstate)
->orWhere('country',$usercountry)->get();
I don't have an idea on what to use for the orderBy part!
You can use
Case
which returns a value on a specified condition, In your case (no pun intended) it returns the order of products.
We will use orderByRaw() to integrate the case statement.
But
you need to be careful because this will cause SQL injection And we don't want that so we use bindings:
Products::where('city',$usercity)
->orWhere('state',$userstate)
->orWhere('country',$usercountry)
->orderByRaw('
CASE
WHEN city = ? THEN 1
WHEN state = ? THEN 2
WHEN country = ? THEN 3
ELSE 4
END',[$usercity,$userstate,$usercountry])->get();
And that will got you the desired result hopefully.
P.S: Follow Laravel naming conventions which says: Model's names should be singular not plural (Product not Products).

Select distinct values based column keys placed in different table

I am new to MySQL I am facing a wall. I was looking for solution but could not find anything which would match my case. I understand how joins working. My question is: Is it possible to do a SELECT based on result from different SELECT?
For clarifying I have 2 tables and 1 view:
report_type table:
id | name | view_name
-------------------------------
1 | citizens | citizens_report
report_filter table:
id | name | filter_column
-------------------------------
1 | City | city
2 | Nationality | nationality
citizens_report view:
id | city | nationality
-------------------------------
1 | Boston | American
2 | London | British
3 | London | Spanish
4 | Paris | French
5 | Paris | French
6 | Boston | German
7 | New York | American
For raportId = 1 I need to look for dynamic view based on result from: report_type table which will be citizens_report - so here is first step where I need to build query with result of it and than I need to create unions of filters based on citizens_report view.
Expected result:
filter | option
----------------------------
city | Boston
city | London
city | Paris
city | New York
nationality | French
nationality | German
nationality | American
nationality | British
nationality | Spanish
I doesn't have to be in any specific order (might be ORDER BY ASC). In any language I can create map where key is equal filter and option is added to array.
Each step I can do with separate query and in any programming language I can call for next one, but could it be done in one query?
Thanks!

SQL: Outputting From Two Tables

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 |
+---------------------+--------+

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.

Insert into MySQL a huge number of rows, based on subquery... having trouble

So, what I am trying to do is insert a row of NONE, $country for every country that exists in the table.
It should look like
Afghanistan, NONE
Albania, NONE
Andorra, None
...
That is, in addition to the provinces listed for each country... they look like this:
| Zambia | Western |
| Zimbabwe | Bulawayo |
| Zimbabwe | Harare |
| Zimbabwe | Manicaland |
| Zimbabwe | Mashonaland Central |
| Zimbabwe | Mashonaland East |
| Zimbabwe | Mashonaland West |
| Zimbabwe | Masvingo |
| Zimbabwe | Matabeleland North |
| Zimbabwe | Matabeleland South |
| Zimbabwe | Midlands
This is the code I am attempting, but failing miserably.
insert into countries2 (province,country)
VALUES ('NONE', (select distinct country from countries2));
I just get
You can't specify target table 'countries2' for update in FROM clause
But it is also throwing the error:
Subquery returns more than 1 row
insert into countries2 (province,country)
select distinct 'NONE', country from countries2
you may want to check the order of the fields !
I'm guessing that you actually just want to update the existing table here?
Try
UPDATE countries2 SET province = 'NONE'