MYSQL Update from Select Join - mysql

Morning everyone,
I have wrote a SQL query to fetch all the data from database I want updated but can work out how i would put an update statement in here.
SELECT product_id, product_title, prodcat_category_id, category_active, product_active from products
LEFT JOIN productcategories on products.product_id = productcategories.prodcat_product_id
LEFT JOIN categories on productcategories.prodcat_category_id = categories.category_id
WHERE category_active = 0
I want to update all these results to have product_active 0, but I've never done an update statement inside of a select join.

Try below query
Update products set product_active = 0 where product_id IN
(
SELECT product_id from products
LEFT JOIN productcategories on products.product_id = productcategories.prodcat_product_id
LEFT JOIN categories on productcategories.prodcat_category_id = categories.category_id
WHERE category_active = 0
);

Instead of selecting using joins, update with the joins.
update products
LEFT JOIN productcategories on products.product_id = productcategories.prodcat_product_id
LEFT JOIN categories on productcategories.prodcat_category_id = categories.category_id
set product_active = 0
WHERE category_active = 0

Related

MYSQL SELECT with INNER JOIN with multiple WHERE/CONDITIONS from the same column

I need to perform a SELECT on the PRODUCTS table
that only returns result if the conditions of the PRODUCTS_DETAILS table are true
follow the sql:
select p.* from products p
join products_details pd
on p.id = pd.id_product
WHERE pd.details = 'quadra mar'
AND pd.details = 'prédio novo'
however when I put two or more "AND" it does not return results
objective: search for all products that have the "where" details
Try,
SELECT p.*
FROM products p
INNER JOIN product_details pd ON p.id = pd.id_product
AND pd.details IN ('quadra mar', 'prédio novo');
Take a look in your WHERE clause, I think an OR condition will fix your issue too.
... WHERE pd.details = 'quadra mar' OR pd.details = 'predio novo'
And if you want only products that contains all the conditions in where clause, you can try:
SELECT pd.id_product, pd.details
FROM products p
INNER JOIN product_details pd ON p.id = pd.id_product
AND pd.details IN ('quadra mar', 'prédio novo')
GROUP BY pd.id_product, pd.details
HAVING COUNT(*) > 1;

subquery error in group by when converting from MySQL to PostgresSQL

I'm converting my database from MySQL to PostgresSQL, and I have this query which tries to sord the products by the cheapest price and the most popular in a given location. It works fine in MySQL, but in Postgres I'm running into problems with this query :
SELECT products.product_id,
suppliers.supplier_id,
product_code.desc_fa,
products.name_fa,
MIN(product_supplier.price) AS price,
SUM(COALESCE(orders.quantity, 0)) AS n_orders
FROM products
JOIN product_code ON product_code.code_id = products.code_id
JOIN product_supplier ON product_supplier.product_id = products.product_id
JOIN suppliers ON suppliers.supplier_id = product_supplier.supplier_id
JOIN product_tags ON product_tags.product_id = products.product_id
JOIN tags ON tags.tag_id = product_tags.tag_id
JOIN product_crop ON product_crop.product_id = products.product_id
JOIN crops ON crops.crops_id = product_crop.crop_id
LEFT JOIN orders
ON orders.product_id = products.product_id and orders.crop_id = product_crop.crop_id
LEFT JOIN user ON user.user_id = orders.user_id and user.location_id = 883
WHERE crops.crops_id = 1
AND product_supplier.quantity >= 3
AND tags.tag = 'علف کش'
GROUP BY products.name_fa
ORDER BY n_orders DESC
LIMIT 10;
It gives me this error :
column must appear in the GROUP BY clause or be used in an aggregate function
Any suggestions to how to work around this error ?
UPDATE :
According to the answers i was able to make it work by using this query:
WITH tem_1 AS (SELECT product_id, MIN(price) AS price FROM product_supplier GROUP BY product_id) ,
tem_2 AS (SELECT product_id, SUM(quantity) AS n_orders FROM orders Group by product_id)
SELECT products.product_id, suppliers.supplier_id, product_code.desc_fa, products.name_fa, tem_1.price,
products.telegraph, suppliers.location_id, COALESCE(tem_2.n_orders,0) AS quant FROM products
INNER JOIN product_supplier ON product_supplier.product_id = products.product_id
INNER JOIN suppliers ON suppliers.supplier_id = product_supplier.supplier_id
INNER JOIN product_code ON product_code.code_id = products.code_id
INNER JOIN product_crop ON product_crop.product_id = products.product_id
INNER JOIN crops ON crops.crops_id = product_crop.crop_id
INNER JOIN product_tags ON product_tags.product_id = products.product_id
INNER JOIN tags ON tags.tag_id = product_tags.tag_id
INNER JOIN tem_1 ON tem_1.price = product_supplier.price AND tem_1.product_id = products.product_id
LEFT JOIN tem_2 ON tem_2.product_id = products.product_id
WHERE crops.crops_id = 1 AND product_supplier.quantity >= 3 AND tags.tag = 'علف کش'
ORDER BY quant DESC
LIMIT 10;;
But since i'm fairly new to SQL, I wanted to know if my code is correct or is there a better way to implement it?
When you use aggregate function (as SUM, MIN and others) the other columns in your field list without aggregate function must be included in GROUP BY clause.
These fields:
products.product_id,
suppliers.supplier_id,
product_code.desc_fa,
products.name_fa
must be in GROUP BY.
Instead your GROUP BY clause has only the following field:
products.name_fa
You must add the other missing 3 fields
In MySql this error has turned off by default, so your query works fine, but in other DBMS you are in an error case.
You can see here how set MySql environment about the GROUP BY behaviour

Using cases to determine which table should join

I have four tables products, product_histories, vendor_invoices and invoices
This is the query I have developed
SELECT p.product_id, product_name, vendor_name FROM products AS p
INNER JOIN product_histories AS ph ON p.product_id = ph.product_id
CASE
WHEN ph.history_type = "P" THEN
LEFT JOIN vendor_invoices AS vi ON link_id = vi.vi_id
WHEN ph.history_type = "S" THEN
LEFT JOIN invoices AS i ON i.invoice_id = link_id
END
ORDER BY ph_id ASC
What I want that if ph.history_type is P then is should join vendor_invoices and if it is S then it should join invoices. But it says there is a syntax error.
Can anyone help me out with it? Or could show a better way to achieve this problem.

mysql #1242 - Subquery returns more than 1 row

This is for my thesis and the dead end is later i don't know what i do wrong here .. Im hoping that someone can help me to know what's wrong here thanks
SELECT
flower_id,
flower_name,
flower_description,
flower_price,
flower_category,
(quantity - (SELECT
SUM(q.quantity_value)
FROM
orders_details od
INNER JOIN
cart_details cd ON cd.cart_id = od.cart_id
INNER JOIN
quantities q ON q.quantity_id = cd.quantity_id
WHERE
od.flag = 1 AND cd.flower_id = flower_id
GROUP BY cd.flower_id)) AS 'quantity',
mfg_date,
exp_date
FROM
flower_details,
categories
WHERE
flower_details.flower_category = categories.category_id
What im doing here is getting the total quantity of products from customer bought minus to inventory stocks
If your subselect return more then a rows you should join the sum using an inner join on subselect
If your subselect return more then a rows you should join the sum using an inner join on subselect inner join on subselect
SELECT
flower_details.flower_id,
flower_name,
flower_description,
flower_price,
flower_category,
flower_details.quantity - t1.quantity,
mfg_date,
exp_date
FROM flower_details
INNER JOIN categories ON flower_details.flower_category = categories.category_id
INNER JOIN (
SELECT cd.flower_id ,
SUM(q.quantity_value) AS quantity
FROM
orders_details od
INNER JOIN
cart_details cd ON cd.cart_id = od.cart_id
INNER JOIN
quantities q ON q.quantity_id = cd.quantity_id
WHERE
od.flag = 1 AND cd.flower_id = flower_id
GROUP BY cd.flower_id
) t1 on flower_details.flower_id = t1.flower_id

Delete multiple join

In my other question I asked how to select a column from multiple tables with inner joins. My new question is: how to delete these results?
SELECT
product_image.image
FROM product
INNER JOIN ixml_prd_map ON product.sku = ixml_prd_map.id_oc_prd
INNER JOIN product_image ON product_image.product_id = product.product_id
WHERE product.model = "xy-type"
If you want to delete only from products then the below should do the job
delete p from product p
INNER JOIN ixml_prd_map ipm ON p.sku = ipm.id_oc_prd
INNER JOIN product_image pi ON pi.product_id = p.product_id
WHERE p.model = "xy-type"
But if you need to delete from all the tables matching the joining condition then use
delete p,ipm,pi from product p
INNER JOIN ixml_prd_map ipm ON p.sku = ipm.id_oc_prd
INNER JOIN product_image pi ON pi.product_id = p.product_id
WHERE p.model = "xy-type"
You can use this query
DELETE FROM product_image WHERE product_image_id IN (SELECT
product_image.image
FROM product
INNER JOIN ixml_prd_map ON product.sku = ixml_prd_map.id_oc_prd
INNER JOIN product_image ON product_image.product_id = product.product_id
WHERE product.model = "xy-type")
EDIT : From the manual
Currently, you cannot delete from a table and select from the same
table in a subquery.
If you want to modify the same query you can execute it by creating a temporary table (here its resultset)
DELETE FROM product_image WHERE product_image_id IN ( SELECT resultset.product_image_id FROM (SELECT
product_image.product_image_id
FROM product
INNER JOIN ixml_prd_map ON product.sku = ixml_prd_map.id_oc_prd
INNER JOIN product_image ON product_image.product_id = product.product_id
WHERE product.model = "xy-type") AS resultset )
OR you can use the USING like this in the example from the MySQL Manual,
13.2.2 DELETE Syntax. I haven't used the USING, but you can definetely check out.
DELETE FROM t1, t2 USING t1
INNER JOIN t2
INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;
Also this SO post will help too MySQL Error 1093 - Can't specify target table for update in FROM clause