Not a Unique table/alias- SQL - mysql

I'm getting the error " Not a Unique table/alias 'Customer' " when I run my join statement. I want to display all the information that I have. I've researched the JOIN statements and I can't find what's wrong.
SELECT Customer.CustomerID, Customer.FirstName, Customer.LastName, Customer.StreetAddress,Customer.City,Customer.State,Customer.Zipcode, Customer.HomePhone,Customer.MobilePhone,Customer.OtherPhone, Pizza.PizzaID,
Pizza.PizzaName, Pizza.Description, Pizza.UnitPrice, OrderInformation.OrderID, OrderInformation.OrderDate, OrderItem.Quantity
FROM Customer
JOIN OrderInformation ON OrderInformation.OrderID = OrderItem.OrderID
JOIN Pizza ON Pizza.PizzaID = OrderItem.PizzaID
JOIN Customer ON Customer.CustomerID = OrderInformation.CustomerID;

SELECT
...
FROM Customer
...
JOIN Customer
You're selecting FROM Customer and then doing JOIN Customer, meaning you now have two instances of Customer. When you reference something like Customer.CustomerID, your query doesn't know which iteration of the Customer table you're referring to.
If you actually needed two references to the same table, you could give one an alias. However, being that you reference an OrderItem table, but never JOIN or select FROM it, I have a hunch that one of those Customer tables should be OrderItem. Perhaps like this...
SELECT ...
FROM OrderItem
JOIN OrderInformation ON OrderInformation.OrderID = OrderItem.OrderID
JOIN Pizza ON Pizza.PizzaID = OrderItem.PizzaID
JOIN Customer ON Customer.CustomerID = OrderInformation.CustomerID;

I would strongly suggest that you use table aliases. It would appear that the first reference to Customer should really be OrderItem:
SELECT c.CustomerID, c.FirstName, c.LastName,
c.StreetAddress, c.City, c.State, c.Zipcode,
c.HomePhone, c.MobilePhone, c.OtherPhone,
p.PizzaID, p.PizzaName, p.Description, p.UnitPrice,
oinf.OrderID, oinf.OrderDate, oi.Quantity
FROM OrderItem oi JOIN
OrderInformation oinf
ON oinf.OrderID = oi.OrderID JOIN
Pizza p
ON p.PizzaID = oi.PizzaID JOIN
Customer c
ON c.CustomerID = oi.CustomerID;

Related

Select values from two mysql tables

I have two tables called Job and Bid . customer can add job details and multiple suppliers can bid for the job.Once customer select one supplier from bids,that supplier id (sId) will update on Job table. i want to select job details with bid value.
This is what i tried
SELECT job.id,job.title,job.desc,job.mobile,job.address, job.city,job.updatedAt,job.status,bid.value,customer.cName,supplier.sName
FROM job
LEFT JOIN customer
ON customer.id = job.cId
LEFT JOIN supplier
ON supplier.id = job.sId
LEFT JOIN bid
ON bid.sId = job.sId`
but this query showing messy information with some duplicates
job Table
bid table
There are two suppliers (sId=2 and sId=1) has bid for job no 5 (i have marked on bid table).but customer has pick sId 2 for the job number 5.now i want to select from those tables without messy records.
I think that you are just missing a join condition on bid, that filters on the chosen supplier. The logic to allow jobs without a supplier yet while evicting jobs whose supplier was not picked by the customer is a bit tricky, and is implemented in the WHERE clause:
SELECT ...
FROM job
INNER JOIN customer
ON customer.id = job.cId
LEFT JOIN supplier
ON supplier.id = job.sId
LEFT JOIN bid
ON bid.sId = job.sId
AND bid.jId = job.id --> here
WHERE supplier.id IS NULL OR bid.id IS NOT NULL
in your select statment you tring to retrive data from bid table bid.value but you didn't join that table.
select j.id,
j.title,
j.desc,
j.mobile,
j.address,
j.city,
j.updatedAt,
j.status,
b.value,
c.cName,
s.sName
from job as j
inner join customer as c
on j.cId = c.id
inner join supplier as s
on j.sId = s.id
inner join bid as b
on j.id = b.jId;

SQL: Problem in selecting multiple tables with foreign key

Problem
I didn't exactly as i want too..
Target to happen
I want to make a transaction view for my database where the output will be:
Tables
Customer Table
Employee Table
Product Table
Transaction Table
Transaction Table
[
Structure I want to happen:
My SQL Query:
SELECT
customer.CustomerName,
transactions.TransactionID,
transactions.Date,
transactions.Type,
transactions.Method,
product.ProductName,
product.Quantity,
product.Price,
employee.EmployeeName
FROM
customer,
transactions,
product,
employee
WHERE
customer.CustomerID = transactions.TransactionID
Query Output:
You must use proper joins (and aliases which are helpful).
The table transactions is linked to all 3 other tables via their foreign keys, so start the joins from it:
SELECT
c.CustomerName,
t.TransactionID,
t.Date,
t.Type,
t.Method,
p.ProductName,
p.Quantity,
p.Price,
e.EmployeeName
FROM transactions t
INNER JOIN customer c ON c.CustomerID = t.CustomerID
INNER JOIN product p ON p.ProductID = t.ProductID
INNER JOIN employee e ON e.EmployeeID = t.EmployeeID

Inner join not working. Output repeating rows

I'm trying to get the the students number & name with the course code & name for students who have a grade below 40. This is what i have
SELECT S.name, S.no, C.code, C.name, T.grade
FROM student S INNER JOIN course C INNER JOIN take T
WHERE grade <40;
It is outputting the grades under 40 but it is returning 128 rows showing everyone's name and number grade repeating them.
Sorry if this is wrong but im a beginner.
You need the conditions that relate the tables to each other:
SELECT S.name, S.no, C.code, C.name, T.grade
FROM student AS s
JOIN take AS t ON t.student_no = s.no
JOIN course AS c ON t.course_code = c.code
Replace student_no and course_code with the actual foreign key columns in the take table.
Simple syntax refer it
SELECT Orders.OrderID, Customers.CustomerName, Shippers.ShipperName
FROM ((Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID)
INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID);

Getting info through 3 tables

I'm following the SQL tutorial from w3schools.
I want to get the value of all orders delivered by a shipper. I don't have any idea about how I can get these details as the info are in different tables and the INNER JOIN didn't worked for me.
Database: http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_groupby
By now, I managed to get the number of orders by each shipper.
SELECT Shippers.ShipperName,COUNT(Orders.OrderID) AS NumberOfOrders FROM Orders
LEFT JOIN Shippers
ON Orders.ShipperID=Shippers.ShipperID
GROUP BY ShipperName;
How could I get the value of those?
To bring the Price of a Product into your query you will need to join in tables OrderDetails to the Order table on the Orders.Id and then join in the Products table to the OrderDetail table on ProductID
SELECT Shippers.ShipperName,
COUNT(Orders.OrderID) AS NumberOfOrders,
Sum(Products.price * OrderDetails.Quantity) AS SumOfPrice
FROM Orders
LEFT JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID
LEFT JOIN OrderDetails ON ORders.OrderID = OrderDetails.OrderID
LEFT JOIN Products ON OrderDetails.ProductID = Products.ProductID
GROUP BY ShipperName;
I just stuck with LEFT JOIN here as your example used, but an INNER JOIN would work just as well and be more efficient.
The FROM clause of the SQL statement is one of the first parts of the SQL to run against your database. It establishes which tables we are grabbing information from and the relationship between those tables (using the ON keyword). So here we bring in 4 tables, and use the ON keyword to show the relationship between all of them using their respective IDs. Then we can add their fields to the SELECT portion of the SQL statement and aggregate where needed.
If you want the "sum" of the product prices, that would be very similar to what you already have. Note how you currently use the COUNT() function to get the count, you can use the SUM() function to get the total of any numeric column.
Something like this:
SELECT
Shippers.ShipperName,
COUNT(Orders.OrderID) AS NumberOfOrders,
SUM(Products.Price) AS PriceOfOrders
FROM
Orders
INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID
INNER JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
INNER JOIN Products ON OrderDetails.ProductID = Products.ProductID
GROUP BY
ShipperName
Or perhaps the price also needs to be multiplied by the quantity in this calculation? Something like this:
SELECT
Shippers.ShipperName,
COUNT(Orders.OrderID) AS NumberOfOrders,
SUM(Products.Price * OrderDetails.Quantity) AS PriceOfOrders
FROM
Orders
INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID
INNER JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
INNER JOIN Products ON OrderDetails.ProductID = Products.ProductID
GROUP BY
ShipperName
It's up to your understanding of the table structure and the data, really. But the concept is the same, grouping by a value and applying a calculation to the grouped values (count or sum).

Query for multiple tables

I'm trying understand how I can pull information from multiple tables at once in one query if that is possible.
I have 3 tables and I'm wondering if there is a way I can query all the product names for customers that live in california?
Table:
products
Fields:
productOid
productName
companyOid
Table:
customerData
Fields:
customerOid
firstName
lastName
state
Table:
orders
Fields:
orderNumber
customerOid
productOid
Would this fall under something like an INNER JOIN?
Also, I'm learning mySQL.
You will need to use inner joins for this.
SELECT DISTINCT p.productName
FROM orders o
INNER JOIN customerData c ON o.customerOid = c.customerOid
INNER JOIN products p ON o.productOid = p.productOid
WHERE c.state = 'CA';
I am using DISTINCT here because it's possible a customer would order the same product more than once (or multiple customers would order the same products) and I'm assuming you don't want duplicates.
I'm also making the assumption that your state is represented as a two character column.
Read more about joins
You could use one more join, but I would write it this way:
SELECT DISTINCT p.productName
FROM
orders o INNER JOIN products p
ON o.productOid = p.productOid
WHERE
o.customerOid IN (SELECT customerOid
FROM customerData
WHERE state = 'California')
It might be a little slover than a join, but it's more readable.
This shows products that CA customers have ordered:
SELECT p.productName
FROM orders o
INNER JOIN products p ON o.productOid = p.productOid
INNER JOIN customerData c ON o.customerOid = c.customerOid
WHERE c.state = 'CA'