SQL update values from table - sql

I'm seriously struggling with some easy stuff.
Currently I have 2 tables (tmp_jmo and persons)
table tmp_jmo
----------
ID ADRESS PERSON_ID
115 Street 1 (null)
120 Street 2 (null)
121 Street 3 (null)
Table persons
ID NAME PERSON_ID
----------
115 John 14
120 Ellen 27
121 Mark 114
Now I want to update the Peson_id from tmp_jmo with the values from person_id (persons table)
In this case I've getting the error that there are to many values
Update tmp_jmo t SET person_id = persons.person_id where tmp_jmo.id = persons.id;
I've also tried to with temporary data but also failing.
I'm sorry to interrupt you with this kind of questions but it's ruining my day!
Many thanks!

In Standard SQL, you can do:
update tmp_jmo t
set person_id = (select p.person_id from persons p where tmp_jmo.id = p.id);
Many databases also support join or from in updates, but that is database-specific syntax.

Related

SQL Join Tables simple approach needed

im looking for a simple logic to merge two tables and expected result is given below.
is it possible to achieve it using SQL join or concatenate commands?
how can i achieve it?
here is the logic I'm working on and trying to find solution:
TABLE 1:
ID Name Title Age
2 Paul Technical Support 22
4 Janne IT Specialist 27
1 Wladimir Team Lead 31
3 Mark Customer Support 40
TABLE 2:
ID Name Title Age
2 Paul Technical Support 22
4 Janne IT Specialist
1 Wladimir 31
3 Mark Customer Support 40
Expected result after merge:
ID Name Title Age
2 Paul Technical Support 22
4 Janne IT Specialist 27
1 Wladimir Team Lead 31
3 Mark Customer Support 40
You should use the following query for this
select t1.id,t1.name,t1.title,t1.age,t2.id,t2.name.t2.title,t2.age from table1 t1 join table2 t2 where t1.id=t2.id

SQL How can this happen? - Query which normally returns 1 result alone actually resulted in multiple results when put inside WHERE clause

Question brief
I'm doing this practice on w3resource and I couldn't understand why the solution worked. I'm 2 days old to SQL. I'll appreciate very much if someone can help me explain.
I have 2 tables, COMPANY(com_id, com_name) and PRODUCT(pro_name, pro_price, com_id). Each company has several products with different prices. Now I need to write a query to display companies' name together with their most expensive products respectively.
The sample answer on the practice is like this
SELECT c.com_name, p.pro_name, p.pro_price
FROM product p
INNER JOIN company c ON p.com_id = c.com_id
AND p.pro_price =
( SELECT MAX(p.pro_price)
FROM product p
WHERE p.com_id = c.com_id );
The query returned expected result.
com_name pro_name pro_price
--------- --------- -----------
Samsung Monitor 5000.00
iBall DVD drive 900.00
Epsion Printer 2600.00
Zebronics ZIP drive 250.00
Asus Mother Board 3200.00
Frontech Speaker 550.00
But I cannot understand how, especially the part inside the bottom sub-query. Isn't SELECT MAX(p.pro_price) supposed to return only 1 highest price of all companies together?
I also tried subsecting this sub-query like this
SELECT MAX(p.pro_price)
FROM product p
INNER JOIN company c ON p.com_id = c.com_id
WHERE p.com_id = c.com_id;
... and it only returned 1 maximum value.
max(p.pro_price)
-----
5000.00
So how does the final result of the whole query include more than 1 records? There's no GROUP BY or anything.
By the way, the query seemed to use 2 conditions for INNER JOIN. But I also tried swapping the 2nd condition into a WHERE clause and it still worked the same. This is one more thing I don't understand.
The databases involved
COMPANY table
COM_ID | COM_NAME
----------------
11 | Samsung
12 | iBall
13 | Epsion
14 | Zebronics
15 | Asus
16 | Frontech
PRODUCT table
PRO_NAME PRO_PRICE COM_ID
-------------------- ---------- ---------
Mother Board 3200 15
Key Board 450 16
ZIP drive 250 14
Speaker 550 16
Monitor 5000 11
DVD drive 900 12
CD drive 800 12
Printer 2600 13
Refill cartridge 350 13
Mouse 250 12
The sub-query is a correlated sub-query. This query is executed for each value of c.com_id in the outer query:
WHERE p.com_id = c.com_id

Join record between table only once in MS Access

I have tbl1 Structured like this
Name Type
====== =====
John 1
David 1
Jane 2
William 3
Alex 2
Ryan 1
And tbl2 structured like this
Index Type Job
1 1 Clean
2 1 Wash
3 2 Carry
4 2 Package
5 3 Sell
I would like to join record with matched Type, but each record in tbl1 only join once with one record in tbl2
Ie:
If
John is joined with Clean then David must be joined with Wash. Or if John is joined with Wash then David must be joined with Clean.
Doesn't matter if David is joined with Wash or Clean , I only need them to be joined with record that match the criteria and be joined ONCE.
I will make sure for each Type in 'tbl1' there will be equivalent amount of record in 'tbl2'
I mainly work on MS Access so Query on this environment would be the best~ Thank you all for reading.
Best regards
Try below query.
select name, (select TOP(1) job from tbl2 where tbl1.type = tbl2.type) from tbl1
Hope it help

Selecting only rows with the highest value of one field, grouped by another field

I have a table that has information structured like this:
ID Points Name School
1 123 James A
2 534 Henry B
3 56 Henry B
4 153 Chris B
5 95 Chris B
6 83 Chris B
7 421 James A
And I need to get out of a query the rows that have the same name, but only the highest points for each like this:
ID Points Name School
7 421 James A
2 534 Henry B
4 153 Chris B
Any ideas on how this could be accomplished with a query? I've spent way too much time trying to figure this out.
select name,school,max(points) from table group by name,school
That will give you the max points per name/school combination. Join it to itself if you want the ID:
select table.* from table inner join
(select name,school,max(points) as points from table group by name,school) a
on a.name = table.name and a.school = b.school and a.points = table.points
edit : sorry, this is a SQL solution...just saw the MSACCESS tag. Logic is right, but you'll need to convert to access syntax.
edit to correct the second query, missed a column inh my join
SELECT
(SELECT TOP 1 ID FROM Table
WHERE
Name = t.Name AND
School=t.School AND
Points=t.Points
) as Id, t.Name, t.Points, t.School
FROM
(SELECT Name, School, max(Points) as Points
FROM Table
GROUP BY Name, School) t

Updating multiple fields in a column simultaneously Microsoft Access

Say I have the following table:
App owner owner_id
______________________
1 John NULL
2 Jack 000123
3 John NULL
4 April 000124
5 John NULL
6 April 000124
7 John 000123
8 Ash NULL
9 Ash NULL
10 Ash NULL
If I need to update John and Ash's owner_ids, is there a way to update each owner's owner_id for every occurrence in the table by only entering the information once?
I have to do this for about 1000 owners, but with the amount of duplicates I am looking at around 10000 blank owner_ids.
I do not have the proper rights to restructure or split the table.
If I didn't misunderstand your question, it's just:
update owners_table set owner_id = '000001' where owner = 'john'
update owners_table set owner_id = '000002' where owner = 'ash'
This will set the owner_id to 000001 in every row where the owner is John, and to 000002 in every row where the owner is Ash.
It's just more copy & paste when you have a lot of owners, because you have to create one query for each owner.
Another way would be to create a temporary table with just the owner and his new id:
owner newid
--------------
John 000001
Ash 000002
You can enter hundreds or thousands of owners and their new owner_ids into the table.
Then, you can update them in your main table all at once with the following query:
UPDATE owners_table
INNER JOIN ownertmp ON owners_table .owner = ownertmp.owner
SET owners_table.owner_id = [ownertmp].[newid];
After that, you can delete the temporary table again - it's just needed to run the query.