I am trying to create a view with the following results. I can create the first part just fine but the second part is where I am having trouble. When I do the join I am receiving an Error Code: 1052. Column 'customer_id' in field list is ambiguous. Now I know that the in the two tables I have duplicate columns.
My question is how do I write the view to ignore one of the duplicate columns
Create a view named customer_addresses that shows the shipping and billing addresses for each customer. This view should return these columns from the Customers table:
customer_id
email_address
last_name
first_name.
This view should return these columns from the Addresses table:
bill_line1
bill_line2
bill_city
bill_state
bill_zip
ship_line1
ship_line2
ship_city
ship_state
ship_zip
The rows in this view should be sorted by the last_name and then first_name columns.
Here is my code.
CREATE OR REPLACE VIEW customer_addresses
AS
SELECT customer_id, email_address, last_name, first_name, addresses.line1
FROM customers JOIN addresses
ORDER BY last_name
I would just identify all the columns like this:
CREATE OR REPLACE VIEW customer_addresses
AS
SELECT
customers.customer_id,
customers.email_address,
customers.last_name,
customers.first_name,
addresses.line1
FROM customers JOIN addresses
ORDER BY customers.last_name
Because I believe that you have an customer_id in the customers table and one in the addresses table. Right?
If you do not specify the table name and add a column to one of the tables with the same column name as in the view the problem will arise again.
Related
I have two tables:
customer, company
The customer table contains company_id and email_address while the company table contains company_id and URL
I'd like to run a query to update all the customer table's company_id to the matching company_id based on a partial match -- specifically if the customer's email_address contains the URL.
For example, if customer has customer#google.com it checks the company table to see if that email contains the URL google.com, and if so, it gives the customer the matching company_id from the company table.
What's an efficient way to do this in single query? Thanks!
I think this would do exactly what you want:
UPDATE customer
LEFT JOIN company
ON (customer.email LIKE CONCAT('%#', company.URL, '%'))
SET customer.company_id = company.company_id
WHERE company.company_id IS NOT NULL;
http://sqlfiddle.com/#!9/89765/1
My table structure is:
Customers(customerid,first name,last name,state)
I want to print the name of the customers who belong to the same state and if they are from states where no other customer lives then that customer should be omitted..I tried inner joins but couldn't get the exact results I get one or more extra rows.
The following is from Access SQL, which should work fine for you.
Select customer_id, state, last_name, first_name
FROM Customers
WHERE (((state) In (Select state FROM Customers GROUP BY state HAVING (((Count(state))>1)))))
ORDER BY state,last_name, first_name
I have two tables users and sellers where user_id and seller_id is different. I want to compare the search query on the first name and the last name of sellers and users and get the results. The two tables have no foreign key associates with it, and on the sellers table its just the seller name. No first name or last name.
SELECT *
FROM users
WHERE first_name LIKE "%'.$search_string.'%"
OR last_name LIKE "%'.$search_string.'%"
that works for the users table.. how to join it with the sellers table?
From the users table I would like to get user_id,first_name,last_name. From the sellers table I would like to get seller_id, seller_name.
I don't quite get what you're trying to accomplish, because you don't mention if there's a joinable field like, for example, transaction id, to relate users and sellers.
If you're searching just any users row or sellers row where the subject matches a search string, you might try with
SELECT 'users' as origin, CONCATENATE('first_name',' ','last_name') as name
FROM users
WHERE (first_name like '%".$search_string."%' OR last_name like '%".$search_string."%')
UNION
SELECT 'sellers' as origin, seller_name as name
FROM sellers
WHERE ( seller_name like '%".$search_string."%')
keep in mind that, if you remove the 'origin' column, the UNION statement will perform an implicit DISTINCT on the resultset. If you need to display dupe results then you should use UNION ALL.
I need to Create a view named customer_addresses that shows the shipping and billing addresses for each customer. This view should return these columns from the Customers table:
customer_id
email_address
last_name
first_name.
This view should return these columns from the Addresses table:
bill_line1
bill_line2
bill_city
bill_state
bill_zip
ship_line1
ship_line2
ship_city
ship_state
ship_zip
The rows in this view should be sorted by the last_name and then first_name columns.
Notes
The customers table includes the following columns: customer_id, email_address, password, first_name, last_name, shipping_address_id, and billing_address_id
The Addresses table includes the following columns: address_id, customer_id, line1, line2, city, state, zip_code, phone
I tried to post a picture of both tables but I am brand new here and do not yet have 10 rep. I am primarily having issues within the join statement due to the conversion of billing_id / shipping_id to actual addresses.
Join the 'addresses' table twice, linking one to the billing address id, and the other to shipping address id.
CREATE VIEW CUSTOMER_ADDRESSES AS
SELECT cust.customer_id, cust.last_name, cust.first_name, cust.email_address
bill.line1 as bill_line1, bill.line2 as bill_line2, bill.city as bill_city,
bill.state as bill_state, bill.zip as bill_zip,
ship.line1 as ship_line1, ship.line2 as ship_line2, ship.city as ship_city,
ship.state as ship_state, ship.zip as ship_zip
FROM customers cust, addresses bill, addresses ship
WHERE cust.shipping_address_id = ship.address_id
and cust.billing_address_id = bill.address_id
There are two tables named Customers and Payments.
They both have the "CustomerNumber"
Here's what i am trying to do.
Select checkNumber, amount, CustomerNumber, CustomerName
FROM Payments, Customers
And i get an error saying : Unknown column....` in 'field list'
I also tried doing this query
Select checkNumber, amount, Payments.CustomerNumber, CustomerName
FROM Payments, Customers
It didn't work T_T
i tried this one
Select checkNumber, amount, customerNumber, customerName
FROM payments, customers
I get this error "Column 'customerNumber' in field list is ambiguous"
This error happends when there're 2 columns with the same name in 2 tables, so you have to specify the same column in which table, i.e :
Select checkNumber, amount, Customers.CustomerNumber, CustomerName
FROM Payments, Customers
or try to make all of your table name and column quoted in ` like this:
Select `checkNumber`, `amount`, `Payments.CustomerNumber`, `CustomerName`
FROM `Payments`, `Customers`
Are your tables named 1 and 2?
If they are, then mysql is probably not recognizing 1 and 2 as table names, but as numbers. Try enclosing the table names with backticks:
select `1`.CustomerName, lastName, street, state
from `1`, `2`
By the way, this will give you every possible combination of rows... be careful (or use a join)
UPDATE
Given the new data in your comment:
Check the field names... field names must be written exactly as the names in the table(s). Notice that you're writing payments.customerNumber in the select section and payments.customersNumber in the from section