I have a table named 'products' and another table named 'rates' that has one to many relation with 'products' table. For each product i have two rows in 'rates' table that i want update one boolean column named 'index' to 1 for each 'product' in 'rates' table.
i used this query :
UPDATE ( SELECT
products.id AS productId,
products.name ,
X.`index` AS `index`,
x.id AS rateId,
x.price, x.discount
FROM products JOIN ( SELECT rates.*
FROM rates
) AS x
WHERE products.id = x.product_id
GROUP BY products.id
) AS y
SET y.index = 1
but id got this error massage:
SQL Error (1288) the target table y of the update is not updatable
i'm new in mysql and i don't know where is my mistake.Thank you for helping
Products Table
| id | name
| 1 | chair
| 2 | bench
Rates Table
| id | product_id | index | value
| 1 | 1 | 0 | xx ==> index = 1
| 2 | 1 | 0 | yy
| 3 | 2 | 0 | zz ==> index = 1
| 4 | 2 | 0 | tt
i want update index column for each product in rates to 1
It looks like you want to update the "first" row in rates for each product_id. If so, you can self-join the table with an aggregate query that computes the minimum id per product_id:
update rates r
inner join (select product_id, min(id) id from rates group by product_id) r1
on r1.id = r.id
set r.index = 1
Related
Read it before marking it as a duplicate question.
There are three tables - invoice_order, invoice_order_item, and stock
and here is the structure of it
invoice_order
order_id | user_id | customer_name | order_date | order_number
1 | 1 | xyz | y-m-d | 0000001
invoice_order_item
Here order_id is a foreign key and order_item_name storing stock_id
order_item_id | order_id | order_item_name | order_item_quantity | order_item_price
1 | 1 | 2 | 5 | 1000
Stock
stock_id | item_type | item_name
2 | goods | Mobile
I'm trying to join these three tables. This is what I achieved till now
$query = "select * from invoice_order inner join invoice_order_item on invoice_order.order_id = invoice_order_item.order_id inner join stock on stock_id = invoice_order_item.order_item_name where invoice_order.order_id = {$order_id}";
The above query doesn't return the order_item_name. It outputs the stock_id instead of the item name. I need to output the name of the item instead of the stock id.
and I know code is vulnerable to SQL Injection. You can ignore it, this is only for testing purpose
SELECT
Stock.item_name
FROM invoice_order
JOIN invoice_order_item
ON invoice_order.order_id = invoice_order_item.order_id
JOIN Stock
ON invoice_order_item.order_item_name = Stock.stock_id
See ref
I have a table user_brand in my database, which stores information about brand which user buy. Assuming that my table has no dependency on another table, table looks like this:
User_ID | Brand | meta_key
---------+--------------+-------------
1 | Killer | Name
1 | Lewis | Name
1 | Pepe | Name
1 | Cloth | Product
2 | Samsung | Name
2 | Motorolla | Name
2 | CellPhone | Product
3 | Acer | Name
3 | Laptop | Product
Now I want my brand column to update so that the output will be like below, and if meta_key is duplicated it will be fine for me, I have not much concern about that.
User_ID | Brand
---------+-----------------------
1 | Killer, Lewis, Pepe
2 | Samasung, Motorolla
3 | Nisaan
I achieved to get the output in select query and i.e
SELECT User_ID, GROUP_CONCAT(Brand)
FROM user_brand
WHERE meta_key = "Name"
GROUP BY User_Id
But tried so many way to update that particular column but failed.
Some tried query is:
UPDATE user_brand
SET Brand = (SELECT max
FROM
(SELECT GROUP_CONCAT(Brand) AS max
FROM user_brand
WHERE meta_key = "Name"
GROUP BY User_id) AS t)
UPDATE user_brand AS t1
JOIN
(SELECT
GROUP_CONCAT(brand) AS max
FROM user_brand
WHERE meta_key = "Name"
GROUP BY User_id) AS t2
SET t1.Brand = t2.max
Any help, it will be highly appreciated.
Try below - You've to add ON Clause
UPDATE user_brand t1
JOIN
(SELECT User_id,GROUP_CONCAT(brand) as max FROM user_brand where meta_key = "Name"
group by User_id
) AS t2 on t1.user_id=t2.user_id
SET t1.Brand = t2.max
This question is based on: Select row from left join table where multiple conditions are true
I am now trying to select rows from Table 1, which do not have a connection in Table 2 to a certain property ID.
These are the tables:
Table 1
| ID | Name |
| 1 | test |
| 2 | hello |
Table 2
| ID | PropertyID |
| 1 | 3 |
| 1 | 6 |
| 1 | 7 |
| 2 | 6 |
| 2 | 1 |
I am using the following query (which is working with '='):
SELECT tab1ID
FROM table2
WHERE propertyID != 3 OR propertyID = 6
GROUP BY tab1ID
HAVING COUNT(*) = 2;
This query should return ID=2, but it returns zero rows. What I am doing wrong?
Any help is greatly appreciated!
Edit: I had given a MWE but this is my actual query:
SELECT transactionline.total FROM transactionline
LEFT JOIN product_variant ON product_variant.SKU = transactionline.SKU
LEFT JOIN product ON product_variant.productID = product.productID
LEFT JOIN connect_option_product ON connect_option_product.productID = product.productID
LEFT JOIN productattribute_option ON productattribute_option.optionID = connect_option_product.optionID
WHERE productattribute_option.optionID = 4 OR productattribute_option.optionID = 9
GROUP BY transactionline.lineID
HAVING COUNT(*) = 1
AND SUM(productattribute_option.optionID = 4) = 0
AND SUM(productattribute_option.optionID = 9) > 0
A product can have multiple connections to the optionID's. The goal of this query is to select the total amount where some filters are true or false.
Your grouping is correct. But you need to count how many times the value you do not want is in your group. That count must be zero.
SELECT tab1ID
FROM table2
GROUP BY tab1ID
HAVING sum(propertyID = 6) > 0
AND sum(propertyID = 3) = 0
I have a table that stores product groups.
table "products_groups"
id | id_group | id_product
============================
1 | 1 | 1
2 | 1 | 2
3 | 1 | 3
4 | 2 | 1
5 | 2 | 2
6 | 3 | 2
I need a SQL query that will find the id_group that contains all id_product's that are in the given array.
product_ids = array(1); // should return no results
product_ids = array(1,2); // should only return id_group 2
product_ids = array(1,2,3); // should only return id_group 1
product_ids = array(1,2,3,4); // should return no results
I played/searched around, ended up stuck at
SELECT p1.id_group
FROM products_groups p1, products_groups p2
WHERE p1.id <> p2.id
AND p1.id_group = p2.id_group
AND (
p1.id_product = 1
OR p1.id_product = 2
OR p1.id_product = 3
)
But it obviously is not giving me the result I am looking for. I don't know if I am thinking too simple or too complex.
Notes: Of course the id_product values will be dynamically generated in the SQL. It will eventually be used with PHP/Codeigniter
Background info: Each product has a price, but products can be in a product group which has a package price. That is why I need to know for each order if the products are in a group.
This problem is called Relational Division
SELECT id_group
FROM products_groups
WHERE id_product IN (1,2)
GROUP BY id_group
HAVING COUNT(*) = 2
SQL of Relational Division
or something like this,
SELECT id_group
FROM products_groups
GROUP BY id_group
HAVING SUM(id_product IN (1,2)) = COUNT(*) AND
COUNT(*) = 2
SQLFiddle Demo
I have a table called user_meta and it contains data like:
---------------------------
| user_id | field | value |
---------------------------
| 1 | 1 | Green |
| 1 | 2 | Square |
| 1 | 3 | Big |
| 2 | 1 | Red |
| 2 | 2 | Square |
| 2 | 3 | Small |
----------------------------
The field column is the number of a form field in the user's profile. The value column is the value the user submitted via the form.
How do I write a MySQL query that returns all users who have 'green big squares'?
Thanks!
This will return the result that you want. This uses a WHERE clause to return all records that have the values that you want, then you count the distinct values to make sure there are only 3:
select user_id
from user_meta
where value in ('Green', 'Square', 'Big')
group by user_id
having count(distinct value) = 3
See SQL Fiddle with Demo
Using a subquery would work if you're stuck with that schema. It's not going to be very fast though.
select userid
from user_meta
where user_id in (
select user_id from user_meta
where (field = 1 and value = 'Green')
)
and user_id in (
select user_id from user_meta
where (field = 2 and value = 'Square')
)
and user_id in (
select user_id from user_meta
where (field = 3 and value = 'Big')
)
SELECT user_id
FROM user_meta user_meta1
JOIN user_meta user_meta2 ON user_meta1.UserID = user_meta2.UserID
JOIN user_meta user_meta3 ON user_meta2.UserID = user_meta3.UserID
WHERE user_meta1.value = 'Green' AND user_meta2.value='Square' AND user_meta3.value='big'