I have to create a view in MySQL, this is the code:
CREATE VIEW dashboard_sales AS (
SELECT o.order_id,o.order_date,o.order_status,op.op_status,oi.oi_qty
FROM
order o
LEFT JOIN
order_items oi
ON
o.order_id = oi.order_id
LEFT JOIN
order_payment op
ON
o.order_id = op.order_id
GROUP BY o.order_id
);
but when I execute in phpmyadmin, I get an error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'order o
LEFT JOIN
order_items oi
ON
o.order_id = oi.' at line 4
How to solve this problem?
You need a backtick for order
CREATE VIEW dashboard_sales AS (
SELECT o.order_id,o.order_date,o.order_status,op.op_status,oi.oi_qty
FROM `order` o LEFT JOIN order_items oi
ON o.order_id = oi.order_id
LEFT JOIN order_payment op
ON o.order_id = op.order_id
);
When you use the keywords for the name of the tables, you should place the table between the Brackets.
CREATE VIEW dashboard_sales AS (
SELECT o.order_id,o.order_date,o.order_status,op.op_status,oi.oi_qty
FROM
[order] o
LEFT JOIN
order_items oi
ON
o.order_id = oi.order_id
LEFT JOIN
order_payment op
ON
o.order_id = op.order_id
GROUP BY o.order_id
);
Related
I need to select the customer and product code and the date on which the order was made, but I'm having some trouble with the join orders.
My SQL select:
select c.customerNumber, p.productCode, o.orderDate as data_compra
from customers as c inner join orders as o
inner join products as p
where p.productCode =
any (
select p2.productCode from products as p2
inner join orders as o
inner join orderdetails as odt
where o.orderNumber = odt.orderNumber and
p2.productCode = odt.productCode
)
and o.orderNumber =
any (
select o2.orderNumber from orders as o2
inner join orderdetails as odt
where o.orderNumber = odt.orderNumber and
p.productCode = odt.productCode
)
Two simple joins should do what you want:
select
c.customerNumber,
d.productCode,
o.orderDate
from customer c
join orders o on o.customerNumber = c.customerNumber
join orderdetails d on d.orderNumber = o.orderNumber
In the code you're asking to inner join two tables but not specifying the relationship. You need to do so SQL can relate and match the rows in each table.
You do this with the ON keyword.
I suggest you watch this video and read this article before continuing
I hope that you can help me with this slightly thorny problem. I am using the W3Schools SQL tutorial and in the process of doing this, I am inventing 'real-world' queries to try and get some experience at this stuff. Using their database, I am trying to find out who ordered what using the following:
SELECT c.CustomerName, p.ProductName
FROM Customers c inner join Orders o on c.CustomerID = o.CustomerID
JOIN OrderDetails od on od.OrderID = o.OrderID
JOIN Products p on p.ProductID = od.ProductID;
This keeps returning the error:
Syntax error (missing operator) in query expression 'c.customerid = o.customerid join orderdetails od on od.orderid = o.orderid join products p on p.productid = od.productid'.
After lots of fiddling about, and having a more experienced colleague look at my query, we can't find what's wrong with what I have written.
Please could you provide me with some help/guidance.
this query is not wrong and not giving any error i'm ruining this query on w3school http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all
open this link and paste the query.
SELECT c.CustomerName, p.ProductName
FROM Customers c inner join Orders o on c.CustomerID = o.CustomerID
JOIN OrderDetails od on od.OrderID = o.OrderID
JOIN Products p on p.ProductID = od.ProductID;
Refer to their documentation here - https://www.w3schools.com/sql/sql_join_inner.asp
Section - "JOIN Three Tables"
If I re-write your code like below then it works fine in W3School's try it editor.
SELECT c.CustomerName, p.ProductName
FROM ( ( ( Customers c inner join Orders o on c.CustomerID = o.CustomerID )
inner JOIN OrderDetails od on od.OrderID = o.OrderID )
inner JOIN Products p on p.ProductID = od.ProductID );
I have added a variable in SSIS and I am trying to add tha variable in my expression query. But it says Expression cannot be evaluated Below is my expression query. Please help if you know where I am making mistakes.
SQL Query
SELECT c.CustomerName, o.OrderID from Customers c
INNER JOIN Orders o ON c.CustomerID=o.CustomerID
Inner Join OrderDetails od ON od.OrderId = o.OrderID
Inner Join Products p on p.ProductID = od.ProductID
where ISNULL(c.IsResult,0) = 0 and o.CompanyID = #CompanyID
Expression Format
"SELECT c.CustomerName, o.OrderID from Customers c
INNER JOIN Orders o ON c.CustomerID=o.CustomerID
Inner Join OrderDetails od ON od.OrderId = o.OrderID
Inner Join Products p on p.ProductID = od.ProductID
where ISNULL(c.IsResult,0) = 0 and o.CompanyID ="+ #[User::intCompanyID]
Let me know if I am doing it right.
I found that casting the numeric values to String or Unicode works. so maybe try
ISNULL(c.IsResult,0) = 0 and o.CompanyID ="+ (DT_WSTR,20) #[User::intCompanyID]
Have you tried the following
"SELECT c.CustomerName, o.OrderID from Customers c
INNER JOIN Orders o ON c.CustomerID=o.CustomerID
Inner Join OrderDetails od ON od.OrderId = o.OrderID
Inner Join Products p on p.ProductID = od.ProductID
where ISNULL(c.IsResult,0) = 0 and o.CompanyID = ?"
NOTE: The image is from a different example
Click on the "Parameters" button and you can then selected your parameter from there.
Hope this helps.
i have 3 tables:
tblproduct (pro_Id, qty, unitprice)
tblorderdetails (order_id, pro_Id, qty)
tblorder (order_id, totalAmount)
i intend joining these tables to as to update the totalAmount in the tblorder table. This is my query using MySql console:
UPDATE o
SET o.totalAmount = p.unitprice * d.qty
FROM tblorder o INNER JOIN tblorderdetails d
on o.order_id = d.order_id
INNER JOIN tblproduct p
on p.pro_Id = d.pro_Id;
This is the error i get:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to use
near 'from tblorder o inner join tblorderdetails d
The syntax is not correct and it should be as
UPDATE tblorder o
INNER JOIN tblorderdetails d
on o.order_id = d.order_id
INNER JOIN tblproduct p
on p.pro_Id = d.pro_Id
SET o.totalAmount = p.unitprice * d.qty ;
As the title says, i am trying to find the customer's who have made orders but have not made payments yet.
I have Three tables;
Customers, Payments, Orders
The sql i have so far gives me (nested query) all the customers without payments, the outer query then tries to join all the customers with orders and checks if those customers are not in my inner table?
SELECT customerWOpayments.customerNumber FROM
ClassicModels.Customers c
INNER JOIN ClassicModels.Orders o ON c.customerName = o.customerNumber
NOT IN
(SELECT distinct c.customerNumber
FROM ClassicModels.Customers c
LEFT OUTER JOIN ClassicModels.Payments p ON c.customerNumber = p.customerNumber
WHERE p.customerNumber IS NULL) customerWOpayments;
I am getting a mysql syntax error at line 8 but cannot figure out why?
This should return customers who have orders but no matching payment assuming all of the keys you joined on in your original example were correct (for example c.customerName = o.customerNumber seems suspicious).
SELECT c.customerNumber
FROM ClassicModels.Customers c
INNER JOIN ClassicModels.Orders o
ON c.customerNumber = o.customerNumber
LEFT OUTER JOIN ClassicModels.Payments p
ON c.customerNumber = p.customerNumber
WHERE p.customerNumber IS NULL;
Basically you missed the WHERE clause. And your question lacks information. Please provide the Schema of your tables. Thanks!
try this one:
Are you sure with this condition ON c.customerName = o.customerNumber?
SELECT customerWOpayments.customerNumber
FROM ClassicModels.Customers c INNER JOIN ClassicModels.Orders o
ON c.customerName = o.customerNumber -- Please check this out
WHERE o.customerNumber NOT IN
(SELECT distinct c.customerNumber
FROM ClassicModels.Customers c LEFT JOIN ClassicModels.Payments p
ON c.customerNumber = p.customerNumber
WHERE p.customerNumber IS NULL);
OR Without Subquery
SELECT a.*
FROM Customers a INNER JOIN Orders b ON
a.CustomerName = b.CustomerNumber -- Please check this line
LEFT JOIN Payments c ON
b.CustomerNumber = c.CustomerNumber
WHERE c.CustomerNumber IS NULL
I believe it's a typo error on a.CustomerName = b.CustomerNumber, instead a.CustomerNumber = b.CustomerNumber
I can't tell exactly if it is because you didn't provide the schema of your tables with some dummy records.
Hope this helps.
Unlike the other solutions, this solution will not produce duplicate customer numbers when customers have more than one order.
SELECT C.customerNumber
FROM ClassicModels.Customers C
WHERE
EXISTS(
-- customer has orders
SELECT *
FROM ClassicModels.Orders AS O
WHERE O.customerNumber = C.customerNumber
)
AND NOT EXISTS(
-- customer does not have payments
SELECT *
FROM ClassicModels.Payments P
WHERE P.customerNumber = C.customerNumber
)