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
Related
I have 2 tables (1) Updates (2) Companies
Updates Table Columns: ID, Title, inserted_at, updated_at, revisions, published_at, archived_at, versions
Companies Table columns: id, name, host, email, inserted_at, updated_at, features.
How do I write a query to show how many posts have been made by a company.
What I know so far is I need to use COUNT in the query but how can I get the no. of updates by a company using that?
SELECT COUNT (column_name)
FROM TABLE (table_name)
condition??
Thanks in advance.
You will have to add a column "company_id" in Updates table which will reference the ID column in Companies table. You will need to add this column then only you can identify which update was made by which company.
So the new structure will be as follows
Companies (ID, name, host, email, inserted_at, updated_at, features)<br/>
Updates (ID, title, inserted_at, updated_at, revisions, published_at, archived_at, company_id)
Then use the following command to select the count of updates
SELECT COUNT(*) FROM updates WHERE company_id = 1;
Or if you want for all the companies then use the following command
SELECT u.company_id, c.name COUNT(u.company_id) FROM companies c, updates u, WHERE c.id = u.company_id GROUP BY u.company_id, c.name;
I have a group of tables that are linked together.
I have a table called Guests and one called Payments.
The Guest table has 3 fields: ID, Firstname, Lastname.
The Payments table has 6 fields: Invoicenumber, ID, Firstname, Lastname, Service, price.
I have it setup, so when I click on ID, it yields me a list of people from the Guests table by their ID, then Firstname, Lastname. So this way I can pick the right ID number to attach to the Payments table. However I can't get the Firstname and Lastname on the the Payments table to populate from the details in the Guest table. How can I do that?
Create combobox with 3 columns, based on Guests table.
In `AfterUpdate' event of this combobox add code for coping of data from 2nd and 3rd columns of combobox to Firstname and Lastname fields of Payments table:
[Firstname] = Me.txtID.Column(1)
[Lastname] = Me.txtID.Column(2)
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 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.
I have this idea which I need some help with. Currently I am developing a mysql database with a java interface. It is a hotel management system, and for billing purpose. I have the following tables.
Bill (BillID, ResvID, IssueDate, ToTalBill, ChargedTo)
Employee (EmpID, Name, Age, Nationality, NatId, Mobile, Telephone, Email, Position, Language, Salary, JoinDate)
User(UserID, Password)
as per the requirements, the ID number of the employee (UserID or EmpID) should be printed on the bill.
Any suggestions or ideas about implementing this ?
Thanx
Note : to log in the system the user has to log in (front desk receptionist). The employee table includes list of all the employees, and there is another table User (who are the users of the system, In other words, User has to be an employee and not all the employees are users )
I am considering linking Bill table to User table
Change your table structure as follows
Bill (BillID, ResvID, IssueDate, ToTalBill, ChargedTo, UserID(foreign key User))
Employee (EmpID, Name, Age, Nationality, NatId, Mobile, Telephone, Email, Position, Language, Salary, JoinDate)
User(UserID, EmpID(foreign Key Employee), Password)
Now you have user id in bill itself and if required you can get empID using userID
Assumptions
- every user is employee
- only users can generate bills.