I have a table that has the rows item_id (unique key), base_id, step_id, and active.
What i need to do is update everything in that table, that matches base_id where step_id = 1 and active = 0 to active = 1 that doesn't have an entry in the same table with the same base_id and step_id = 2 and active = 1.
----------------------------------------
| item_id | base_id | step_id | active |
| 1 | 1 | 1 | 0 |
| 2 | 1 | 2 | 1 |
| 3 | 2 | 1 | 0 |
| 4 | 3 | 1 | 0 |
| 5 | 3 | 1 | 0 |
----------------------------------------
This would return make item 3, 4, and 5 update to have active = 1
If that makes sense. Any help is greatly appreciated. Thanks in advance.
select item_id
from your_table
where base_id in
(
select base_id
from your_table
group by base_id
having sum(step_id = 1 and active = 0) > 0
and sum(step_id = 2 and active = 1) = 0
)
What does the inner select do?
It groups the records by base_id and takes only those having at least 1 record with step_id = 1 and active and zero records with step_id = 2 and active = 1.
sum() counts how many times the inner condition is true.
To update the table so that the matching values have active set to 1 we can take the solution by juergen d and turn it into an update statement.
As MySQL has some issues with updating a table that it is referencing in a subquery we insert an extra level of nesting which forces the creation of a temporary result and allows the update:
update table1 t
set t.active = 1
where base_id in (
select base_id from (
select base_id
from table1
group by base_id
having sum(step_id = 1 and active = 0) > 0
and sum(step_id = 2 and active = 1) = 0
) a
);
This would set active = 1 for item_id 3, 4 and 5
Sample SQL Fiddle
I would approach this with a simple in or exists:
select t.*
from table t
where step_id = 1 and active = 0 and
not exists (select 1
from table t2
where t2.base_id = t.base_id and t2.step_id = 2 and t2.active = 1
);
This seems like a direct translation of your description.
Related
I have a table where I store data for different groups and I need to update one group if values in one column are matching.
the table looks like this:
prop_id | group_id | value | visible
1 | 1 | 10 | 1
1 | 2 | 10 | 1
1 | 3 | 15 | 1
2 | 1 | 10 | 1
2 | 2 | 10 | 1
2 | 3 | 10 | 1
So I want to set the visible column to 0 for the group_id=3 if the values in the value column are equal to group_id=1. In this case if value=10 for both group_id=1 and group_id=3 than set visible=0 for group_id=3
expected result after update
prop_id | group_id | value | visible
1 | 1 | 10 | 1
1 | 2 | 10 | 1
1 | 3 | 15 | 1
2 | 1 | 10 | 1
2 | 2 | 10 | 1
2 | 3 | 10 | 0
How is this possible?
Write it as a SELECT first.
Start simple, the rows that we want to update we know are group_id=3 and visible=1, so write a query that gets all of those rows:
SELECT g3.value
, g3.visible
FROM mytable g3
WHERE g3.group_id = 3
AND g3.visible = 1
We know the rows we want to update are in that set, but there are some additional conditions.
So we extend that. According to the spec, we need to find out if there are any matching group_id=1 rows that are visible=1 (matching on value).
We can do that check either with an EXISTS correlated subquery, or we can use a JOIN.
SELECT g3.group_id
, g3.value
, g3.visible
FROM mytable g3
WHERE g3.group_id = 3
AND g3.visible = 1
AND EXISTS ( SELECT 1
FROM mytable g1
WHERE g1.group_id = 1
AND g1.visible = 1
AND g1.value = g3.value
)
-or-
SELECT g3.group_id
, g3.value
, g3.visible
FROM mytable g3
JOIN mytable g1
ON g1.group_id = 1
AND g1.visible = 1
AND g1.value = g3.value
WHERE g3.group_id = 3
AND g3.visible = 1
Verify the query is returning the rows we want to update, under the specific conditions. (It is much easier to verify the results of a SELECT statement, and adjust as necessary, than it is an UPDATE statement.)
Once we have a SELECT query working and verified (returning the rows we want to update) we can convert it into an UPDATE statement. Replace the SELECT ... FROM with UPDATE and add a SET clause that is returning the rows
UPDATE mytable g3
JOIN mytable g1
ON g1.group_id = 1
AND g1.visible = 1
AND g1.value = g3.value
SET g3.visible = 0
WHERE g3.group_id = 3
AND g3.visible = 1
Use Self JOIN then UPDATE
You can try this.
UPDATE T t1
JOIN T t2 on t1.group_id = t2.group_id
and t1.rop_id<>t2.rop_id and t1.value > t2.value
SET t2.visible = 0
sqlfiddle:http://sqlfiddle.com/#!9/6f06de/1
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
ID stone_free original_stone_id
------- | ---------- | -------------------
1 | 0 | 1
2 | 1 | 2
3 | 1 | 1
I would like to return rows from the table only if stone_free equals 0 and there is no corresponding original_stone_id in other rows for each row. So for example in the above example row 1 has stone_free as 0 and there is a corresponding original_stone_id in row 3, therefore the query shouldn't return any rows.
In the example below row 1 has stone_free as 0 but there is no corresponding original_stone_id in the other row, therefore the query should return row 1.
ID stone_free original_stone_id
------- | ---------- | -------------------
1 | 0 | 1
2 | 1 | 2
A simple LEFT JOIN should do it;
SELECT a.*
FROM Stones a
LEFT JOIN Stones b ON a.ID<>b.ID AND a.original_stone_id = b.original_stone_id
WHERE b.ID IS NULL AND a.stone_free=0
Demo here.
I'm not sure if it'll work on MySQL but give a shot to it
select *
from tbl
where stone_free = 0 and
(
select count(tab.original_stone_id)
from tbl tab
where tab.original_stone_id = tbl.original_stone_id
) = 1
Select * from tablename t1
Where stone_free = 0
And Not Exists (Select Id from tablename t2 where t2.original_stone_id = t1.original_stone_id And t2.Id <> t1.Id)
I have a 4 column table, the primary key is a composite of id, gameId and flag.
id | gameId | score | flag
--------------------------
1 | 1 | 10 | 1
1 | 1 | 20 | 0
2 | 1 | 1 | 0
1 | 2 | 10 | 1
3 | 2 | 1 | 0
I need to update the table so that:
All of gameId 2 scores are added to gameId 1 where the id and flag is the same.
(e.g. row 1 would have a score of 20 by adding the scores of row 1 and row 4)
If the above happens the gameId 2 row needs to be deleted.
Where the above isn't found (e.g. there is a row where the gameId is 2 but the id and flag don't match another row), the gameId can just be changed to 1.
So my table after the SQL is complete should look like this:
id | gameId | score | flag
--------------------------
1 | 1 | 20 | 1
1 | 1 | 20 | 0
2 | 1 | 1 | 0
3 | 1 | 1 | 0
How can I write this in SQL?
Thanks :)
think this going to work:
Try 1
UPDATE score_list AS t2
LEFT JOIN score_list AS t1 ON
(t1.id = t2.id AND t1.flag = t2.flag AND t1.gameId = 1)
SET
t1.score = t1.score + t2.score,
t2.gameId = IF(t1.gameId IS NULL, 1, t2.gameId)
WHERE t2.gameId = 2;
DELETE FROM score_list WHERE gameId = 2;
Try 2
# add scores to gameId = 1
UPDATE score_list AS t2
LEFT JOIN score_list AS t1 ON
(t1.id = t2.id AND t1.flag = t2.flag AND t1.gameId = 1)
SET
t1.score = t1.score + t2.score
WHERE t2.gameId = 2;
# try to move gameID 2 to gameId 1, using ignore to allow rows to fail, as gameId alredy exists
UPDATE IGNORE score_list SET gameId = 1 WHERE gameId = 2;
# delete all not updated rows from last query
DELETE FROM score_list WHERE gameId = 2;
I want to count the number of records in database from more than two tables that are joined.
For example I have a table like this.
table
jobd + name
1 | jobA
2 | jobB
tableA
imgeid + orderid + jobid
1 | 1 | 1
2 | 2 | 1
3 | 3 | 1
4 | 4 | 1 (this order is not yet started)
tableB
taskid + orderid + task + status
1 | 1 | 1 | UPDATED
2 | 1 | 1 | UPDATED
3 | 1 | 1 | COMPLETED
4 | 2 | 2 | SAVED
5 | 3 | 3 | COMPLETED
My problem here is that when I count base on status (# tableB) my query results both the UPDATED which has the same orderid.
This is my sample query that same with the one I'm working.
SELECT t.name
COUNT(CASE WHEN tb.task = 1 AND tb.status <> 'COMPLETED' THEN tb.status ELSE NULL END) inprogress,
COUNT(CASE WHEN tb.task = 1 AND tb.status = 'COMPLETED' THEN tb.status ELSE NULL END) completed
FROM tableA ta
LEFT JOIN tableB tb
ON tb.orderid = ta.orderid
LEFT JOIN table t
ON t.jobid = ta.jobid
GROUP BY t.jobid;
My results something like
name + inprogress + completed
jobA | 2 | 1
The inprogress results must only be 1 because it has the same orderid. The reason why it has two UPDATED because this table is HISTORICAL. I don't know how can get the distinct orderid in tableB so it will only results to 1.
The main point here is that I can count the total orders which status is in progress, completed and not started per job.
I hope my question is clear. If you have other way, please let me know. Thanks
Can't you use a Count distinct? Here's a link, see nearer the bottom of the page, it will only the unique field you specify: w3schools.com/sql/sql_func_count.asp
SELECT t.name
COUNT(DISTINCT tb.orderid CASE WHEN tb.task = 1 AND tb.status 'COMPLETED' THEN tb.status
ELSE NULL END) inprogress,
COUNT(DISTINCT tb.orderid CASE WHEN tb.task = 1 AND tb.status = 'COMPLETED' THEN tb.status
ELSE NULL END) completed