I have two tables, customer and newCustomer. For specific columns, I want to overwrite the column values of customer table with the newCustomer table. For example :
customer.firstname = newCustomer.firstname
customer.lastname = newCustomer.lastname
** I do have matching Ids for the two tables.
I can think of how to do this in terms of coding, but am having a hard time when thinking of doing this in SQL.
I am using Microsoft SQL Server.
Would appreciate any examples or hints.
Presumably, you have some id that connects the two tables. If so, just use join and set:
update c
set firstname = nc.firstname,
lastname = nc.lastname
from customer c join
newcustomer nc
on c.customerid = nc.customerid;
Related
Hi guys I'm new with databases and I'm trying to make a query where I join 3 tables. I could make it and I want to clean up the result. I want to know how can I delete the column "pin" from users table and maybe some "ids" columns.
Select * from "wish-list"
Join products
On "wish-list".id = products.holiday_id
Join users
On "wish-list".user_id = users.id
Where "wish-list".id = 1
You need to specify which columns you really need in your output. At the moment you are using
SELECT * which outputs all columns of all joined tables.
Here is what it should look like:
SELECT holiday, products.description, users.pin FROM "wish-list"
JOIN products ON "wish-list".id = products.holiday_id
JOIN users ON "wish-list".user_id = users.id
WHERE "wish-list".id = 1
It's important that you reference all columns which are not your main entity (here wish-list) with tablename.column (products.description and not only description). It will work without referencing strictly but only if the column name is unique in your query.
Furthermore you can rename columns. This is useful for example if you want to get the id's of the product table and the wish-list table.
SELECT product.id AS product_id, id AS wishlist_id FROM "wish-list"
...
Hope that helps!
I have following tables: Product and ProductEvent. Some of the relevant columns of Product table are SocialID, MarketID, LocalID. These three columns have numbers that corresponds to one column called EventID in ProductEvent.
Here is my question: is there a better way to join these tables where I do not have to make 3 separate joins all pointing to match EventID?
Maybe something like this:
select *
from Product P join ProductEvent PE
on (PE.EventID = P.SocialID or PE.EventID = P.MarketID or PE.EventID = P.LocalID)
So I have three tables: companies, addresses and company_address.
For optimization reasons I need to copy city column from addresses table to companies table. Relation between companies and addresses is many to one (as many companies can occupy same address). They are connected through company_address table, consisting of address_id and company_id columns.
I found this solution for case without intermediate table: How to copy one column of a table into another table's column in PostgreSQL comparing same ID
Trying to modify query I came up with:
UPDATE company SET company.city=foo.city
FROM (
SELECT company_address.company_id, company_address.address_id, address.city
FROM address LEFT JOIN company_address
ON address.id=company_address.address_id
) foo
WHERE company.id=foo.company_id;
but it gives error:
ERROR: column "company" of relation "company" does not exist
I cant figure out what is going on. I'll be grateful for any ideas.
You don't need a subquery for that. Also, refer in the SET clause to your table columns without preceding with table name.
I believe that since your WHERE condition includes joined table, it should be INNER JOIN instead of a LEFT JOIN.
UPDATE company c
SET city = a.city
FROM address a
INNER JOIN company_address ca ON a.id = ca.address_id
WHERE c.id = ca.company_id
Note how using aliases for table names shortens the code and makes it readable at the very first glance.
You're right syntactically, you just don't need the table name at the beginning of the update statement:
UPDATE company SET city=foo.city
FROM (
SELECT company_address.company_id, company_address.address_id, address.city
FROM address LEFT JOIN company_address
ON address.id=company_address.address_id
) foo
WHERE company.id=foo.company_id;
Let's say we have table customer with column subject_type_fk. We need to find name of the customer which can be in table person or enterprise. subject_type_fk defines which of those 2 tables search in. subject_type_fk can be 0 or 1. subject_fk defines the primary key of record from person or enterprise. Both tables have name column we need to retrieve (actually enterprise.enterprise and person.last_name). Customer.customer is given as input parameter. How to write that kind of query for postgreSQL?
UPDATE
select p.last_name
from customer c join person p on c.subject_fk = p.person and c.subject_type_fk = 0
union
select e.full_name
from customer c join enterprise e on c.subject_fk = e.enterprise and c.subject_type_fk = 1
(edited after OP's edit)
i hava a set of following tables
customer(cus_id,cus_name);
jointAccount(cus_id,acc_number,relationship);
account(acc_number,cus_id)
now i want to create a select statement to list all the jointAccounts,
it should included the both customer name, and relationship.
I have no idea how to retrieve both different user name, is that possible to do this?
Generally speaking, yes. I'm assuming you mean you want to get customer info for both sides of the joint account per your jointAccount table. Not sure what database you're using so this answer is assuming MySQL.
You can join on the same table twice in a single SQL query. I'm assuming you have not yet created your tables, as you have cus_id listed twice in the jointAccount table. Typically these would be something like cus_id1 and cus_id2, which I've used in my sample query below.
Example:
SELECT c1.cus_id AS cust1_id, c1.cus_name AS cust1_name
, c2.cus_id AS cust2_id, c2.cus_name AS cust2_name, j.relationship
FROM customer c1
INNER JOIN jointAccount j
ON c1.cus_id = j.cus_id1
, customer c2
INNER JOIN jointAccount j
ON c2.cus_id = j.cus_id2
I haven't tested this but that's the general idea.
try this query:
SELECT * FROM jointAccount a LEFT JOIN customer c ON a.cus_id = c.cus_id;
just replace the * with the name of the columns you need.