I need help with an SQL query I am trying to execute. I have two MYSQL tables as follows:
Table 1: Orderdetails with column name QtyOrdered
and
Table 2: Titles with column name No_of_copies_in_inventory
I would like to perform a SQL query where I can effectively update the No_of_copies_in_inventory in Titles table and do this
No_of_copies_in_inventory in Titles = No_of_copies_in_inventory - QtyOrdered
Any input/feedback would be greatly appreciated.
Thanks!
Assuming a uniquely indexed field (such as ID), you could do it this way:
UPDATE Orderdetails od
INNER JOIN Titles t ON od.id = t.id
SET t.No_of_copies_in_inventory = (t.No_of_copies_in_inventory - o.QtyOrdered)
WHERE od.id = 'idhere'
However, chances are you will have a variable stored for QtyOrdered and could simply use that rather than joining the tables
Related
I Have two tables. The first is prodstock, and the second is orderDetails: follows:
prodstock table
OrderDetails Table
When I change the status of the order table and deliver, the quantity in my product table should decrease by the quantity in the order table, but I have no idea how to construct a query that does that.
I made this query which works for me:
UPDATE prodstock
INNER JOIN orderdetails ON prodstock.cat_id = orderdetails.cat_id AND
prodstock.dyenumber = orderdetails.dyenumber
SET prodstock.total_stock = prodstock.total_stock - orderdetails.qty
WHERE orderdetails.order_id = 2;
I have achieved my desired query but I want to know how this one worked. I have multiple tables on my database and my requirements was to take the id from table called product and using this id, I want to retrieve some data from multiple tables and product id is a foreign key to the other tables. The query below works fine (by the way I was just experimenting and luckily got this query).
SELECT ponsfdp.*, product.pName, product.pImage, product.productSizes FROM product
INNER JOIN priceOnSizesForDigitalPrinting AS ponsfdp ON ponsfdp.pId_fk =
(SELECT pId FROM product WHERE pName LIKE "%booklet%")
WHERE pName LIKE "%booklet%";
But when I tried this query,
SELECT ponsfdp.*, product.pName, product.pImage, product.productSizes FROM product
INNER JOIN priceOnSizesForDigitalPrinting AS ponsfdp ON ponsfdp.pId_fk =
(SELECT pId FROM product WHERE pName LIKE "%booklet%");
It contains all the data even with null fields too. Can someone explain to me how it works? My personal opinion is both query should return same data because on the second query, I am using a subquery and it returns only one id, on the other hand, first query has a WHERE clause which generates the same id but by the help of name. How does the first query returns very specific columns and second return all columns even null columns too? I need an explanation for both queries.
Your first query also returning all rows as returned from your second query. But, when you are adding the last filter-
WHERE pName LIKE "%booklet%"
It's just keeping one single row from all rows where pName is like 'booklet'. You can consider the output from your second query as a single table and your logic working as below-
SELECT * FROM (
SELECT ponsfdp.*, product.pName, product.pImage, product.productSizes
FROM product
INNER JOIN priceOnSizesForDigitalPrinting AS ponsfdp
ON ponsfdp.pId_fk = (SELECT pId FROM product WHERE pName LIKE "%booklet%")
)A
WHERE pName LIKE "%booklet%"
Hope this will at least give you some insight of your query.
I don't see any need for a subquery here. You should be using the where condition to select rows from your FROM table, then use the ON clause of your join to find the right record(s) in your joined table for each row of the FROM table:
SELECT ponsfdp.*, product.pName, product.pImage, product.productSizes
FROM product
INNER JOIN priceOnSizesForDigitalPrinting AS ponsfdp
ON ponsfdp.pId_fk = pId
WHERE pName LIKE "%booklet%";
Im currently writing a stored procedure in SQL to print results from multiple tables to find the top ten products purchased, but I am getting the syntax error
"Invalid column name 'ProductID'".
This appears on the 2nd INNER JOIN statement at sod.ProductID
My code below
CREATE PROCEDURE usp_top10ProfitableProducts
AS
BEGIN
SELECT TOP 10 sp.StoreProductID, sup.ProductName, sum(sod.Quantity) AS quantitysold, (sum(sod.Quantity) * sum(sod.unitPrice)) - (sum(sod.Quantity) * sum(sp.costPrice)) AS Profit
FROM SalesOrderDetails sod
INNER JOIN StoreProduct sp ON sp.StoreProductID = sod.StoreProductID
INNER JOIN SupplierProduct sup ON sup.ProductID = sod.ProductID
WHERE Quantity > 0
END
Thanks in advance.
EDIT** Below is also my Entity Relationship diagram. 'ProductID' lives in 'SupplierProduct'
Probably the field does not exist in one of the tables. Another point: group by was missing.
GROUP BY sp.StoreProductID, sup.ProductName
To do a join between two tables the joining column (in above case ProductID) should be available in both tables, otherwise sql will not be able to do the join.
According to the table structure SalesOrderDetails doesn't have a column ProductID which is why it's giving the error, you need to the join on cloumns that exist in the respective tables
I need to write a query to join 3 tables.
My tables are:
ucommerce_customer
ucommerce_order
ucommerce_order_line
All 3 tables have a column called order_id.
The table ucommerce_order has a column called order_status.
When the order_status is set to "open" I want to display the order details.
ResultSet myRs = myStmt.executeQuery
("SELECT * FROM ucommerce_customer
INNER JOIN ucommerce_order
INNER JOIN ucommerce_order_line
WHERE ucommerce_order.order_status = 'open'");
My query ignores the order status and displays all orders, open and closed.
Also I have several products so ucommerce_order_line has several entries for the same order_id, my query displays duplicate entries and it duplicates the entire list as well.
How can I write a query that will show only open orders without duplicating everything?
In MySQL, the on/using clause is optional. This is very sad because someone can make mistakes like you did. Your question only mentions one column, so perhaps that is all that is needed for the join:
SELECT *
FROM ucommerce_customer INNER JOIN
ucommerce_order
USING (orderId) INNER JOIN
ucommerce_order_line
USING (OrderId)
WHERE ucommerce_order.order_status = 'open';
I would be surprised if the customer table really had a column called OrderId (seems like a bad idea in most situations), so the first USING clause might want to use CustomerId.
I would recommend to use a natural join instead. Maybe that's where the errors are coming from.
The duplicates can be removed by running SELECT DISTINCT * ...
I've created several tables in a test database based on columns/rows in my main database.
test.websites.url = main.websites.url
test.category_main = main.websites.category1
test.category_01 = main.websites.category2
test.category_02 = main.websites.category3
etc...
The test database columns already contain all the rows from the main
database, but I need to add the rows from the respective tables to the
category_to_website table and create foreign keys because there is
currently no relation between them in the test database. That is why I have joined the
main database in the query.
When trying to use the main table as a reference for updating the existing rows in the test database, some values are updated but they are not always correct. I'm executing the query from the test database.
My query:
UPDATE category_to_website
LEFT JOIN main.websites
ON websites.url = main.websites.url
LEFT JOIN category_01
ON category_01.name = main.websites.category2
SET category_to_website.category_01_id = category_01.id
WHERE category_to_website.category_01_id = main.websites.category2
My database schema:
I suspect that the issue is with the type of JOINs I am doing, but I've tried LEFT JOIN, JOIN, and INNER JOIN and get the same results. I think that maybe I need a SELECT sub query or my WHERE clause is off?
EDIT
Based on the comments I was able to get this all sorted out. Here are the steps I took.
1. Merged the category_* tables into a category table.
2. Joined the test.websites table into the query.
UPDATE test.category_to_website
LEFT JOIN test.websites
ON test.websites.id = category_to_website.url_id
RIGHT JOIN main.websites
ON test.websites.url = main.websites.url
INNER JOIN test.category
ON test.category.name = main.websites.category1
SET category_to_website.category01_id = category.id
WHERE category_to_website.url_id = test.websites.id
UPDATE category_to_website
JOIN websites
ON websites.id = category_to_website.url_id
JOIN main.websites
ON websites.url = main.websites.url
JOIN category
ON category.name = main.websites.category1
SET category_to_website.category01_id = category.id
SETs are done to rows that participate in the JOINs. But if you LEFT JOIN to category_to_website then all its rows participate so then you must restrict them in a WHERE the way you already did in the ON.
Thank goodness you have started to relationalize that horrible schema. Keep going: replace all multiple categery_ columns in each table by just one category column for id or name. And if you named them category_id and category_name their nature would be clear. (Maybe post your next version as a new question.)