I have created a view via:
CREATE VIEW product_prices
AS SELECT Name, Price
FROM Products;
And I need to update it so prices under £10 are made 10% bigger apart from ones that are in category 3. These need to be looked up in a separate SELECT statement.
I have got this code so far but I'm getting the 1443 error that the definition of the view won't let me update it. How do I make it an updatable view?
UPDATE product_prices
SET Price = Price * 1.1
WHERE Name != (SELECT Name FROM Products WHERE Category_ID = 3)
AND Price < 10;
Use a join instead of <>:
UPDATE product_prices pp LEFT JOIN
Products p
ON p.name = pp.anme and p.Category_ID = 3
SET pp.Price = pp.Price * 1.1
WHERE p.Name IS NULL AND pp.Price < 10;
The issue is the operation on the table being updated, not the view definition.
Related
I'm a bit of a noob and I'm sure this is fairly simple but I've tried different things and I can't get my head around it.
I have a product table that contains a category id and I want to set the quantity to 0 which is stored in another table.
This is the last thing I tried:
function wipeprod(){
UPDATE stock_available
INNER JOIN
product stock_available
ON
(
product.id_category_default != 3
OR
product.id_category_default != 10
OR
product.id_category_default != 943
)
SET
stock_available.quantity = 0;
}
Thanks in advance.
I suspect that you want to set column quantity in table stock_available to 0 for products that are not in categories 3, 10, and 943.
If so, you can use the update ... join syntax as follows:
update stock_available s
inner join product p on p.product_id = s.product_id
set s.quantity = 0
where p.id_category_default not in (3, 10, 943)
The main problems with your original query are:
the join is missing a predicate on product_id
you wanted AND condition rather than OR; NOT IN comes handy to shorten this.
You can use such an update statement by use of INNER JOIN :
UPDATE stock_available s
JOIN
(
SELECT id
FROM product
WHERE id_category_default NOT IN (3,10,943)
) AS p ON p.id = s.product_id
SET
s.quantity = 0;
where no need a proper query after the keyword JOIN
I need to optimise my code.it works but takes time and sometimes timeouts.
Objective selected columns from table 1 and table 2 must be combined in another table. duplicates are not allowed in the new table. TIA
$modelsc=Customers::find()->select('customer_id')->all();
$modelsp = Product::find()->select('product_no')->all();
foreach($modelsc as $modelc) {
$user = $connection->createCommand(
'SELECT product_no as product_no,:cust_no as fkcustomer_id
FROM product AS p
WHERE NOT EXISTS( SELECT pc.fkproduct_no
FROM
productcustomer AS pc
WHERE
pc.fkproduct_no = p.Product_no AND fkcustomer_id = :cust_no)');
$user->bindValue(':cust_no', $modelc->customer_id);
$modelsx = $user->queryAll();
Yii::$app->db->createCommand()->batchInsert('productcustomer', [ 'fkproduct_no', 'fkcustomer_id'], $modelsx)->execute(); }
looking to your code you could avoid the not exists clause an try using a left join checkn for null on pc.fkproduct_no
SELECT product_no as product_no,
:cust_no as fkcustomer_id
FROM product AS p
LEFT JOIN productcustomer AS pc ON pc.fkproduct_no = p.Product_no
AND fkcustomer_id = :cust_no
WHERE pc.fkproduct_no is null
Anyway be sure you have proper index on the column where of join condition
for table products an index on column Product_no
for table productcustomer a composite index on (fkcustomer_id, fkproduct_no)
I'm wanting to do the following:
Select everything from product that matches the value of type_id from both tables product and system_type. Then with those matched results, match cat_id from both tables system_type and system_cat and then refine the final result where cat_type = 0 from the system_cat table.
Current SQL seems to have a syntax error:
SELECT * FROM product
JOIN system_type
USING (type_id)
JOIN system_cat
USING (cat_id)
WHERE cat_type = 0
What else I've tried:
SELECT * FROM product
JOIN system_type
USING system_type.type_id = product.type_id
JOIN system_cat
USING system_type.cat_id = system_cat.cat_id
WHERE system_cat.cat_type = 0
Try this. You may need to explicitely type out the columns you need
SELECT * FROM product as pr
INNER JOIN system_type as st
ON st.type_id = pr_id
INNER JOIN system_cat as sc
ONH st.cat_id = sc.cat_id
WHERE sc.cat_type = 0
Syntax changes when using table_name.column method. Use ON and not USING.
SELECT * FROM product
JOIN system_type
ON system_type.type_id = product.type_id
JOIN system_cat
ON system_type.cat_id = system_cat.cat_id
WHERE system_cat.cat_type = 0
Is it possible to do a select and join a table, then update that table?
I'm basically looking to increase the price by 5% on all products owned by a particular supplier. The problem is the pricing details are held in a separate table to the products themselves.
I know this syntax is not correct, but it will give you the gist of what i'm trying to achieve:
update
products_quantity_pricing set price = price + (price/100*5)
where (select
products.supplier_id,
products_quantity_pricing.price
from products
join
products_quantity_pricing on products_quantity_pricing.product_id = products.id
where products.supplier_id = 7 )
You put the JOIN directly into the UPDATE query:
UPDATE products_quantity_pricing AS pqp
JOIN products AS p ON pqp.product_id = p.id
SET price = price + (price/100*5)
WHERE p.supplier_id = 7
update products_quantity_pricing set price = price + (price/100*5)
where products_quantity_pricing.price in
(select products_quantity_pricing.price
from products join
products_quantity_pricing on products_quantity_pricing.product_id = products.id
where products.supplier_id = 7 )
I wish to populate a table on in a mysql database. Firstly I'd like to pull back all of the possibilities and them trim out the unrequired ones (easier than just adding them by hand).
The final table is:
combinations
combID
productID
type
content
exclude
extrafield2
extrafield6
The data comes from
extrafields_values
exvalueID
productID
extrafieldID
content
For each product I need to get return a row for each combination in extra_field_values (extrafieldID = 2 and extrafieldID = 6)
For instance:
productID = 700
extrafield2 = E, D, F
extrafield6 = 34,35,36,37
Returns the exvalueID to extrafields2 and 6 for each combination
So far I've tried:
SELECT EV.productID, extraFieldID, content AS extrafield6,
(SELECT content AS extrafield2
FROM wjf_extrafields_values AS EV2
INNER JOIN wjf_products AS P2
WHERE extraFieldID = 6) AS extrafield2
FROM wjf_extrafields_values AS EV
INNER JOIN wjf_products AS P ON P.productID = EV.productID
WHERE extrafieldID = 6
I believe you just need to link your wjf_products table to your extrafield_values table twice as shown below.
select p.productID, ev1.content as extrafield2, ev2.content as extrafield6
from wjf_products p inner join extrafields_values ev1 on p.productID = ev1.productID
inner join extrafields_values ev2 on p.productID = ev2.productID
where ev1.extraFieldID = 2
and ev2.extraFieldID = 6
Why not just create and use two views instead of a query.
View1 = Combinations
View2 = Subset of View1
Insert data using the View2