Select from table, update the rows selected - mysql

I am trying to write a query that selects values from certain rows based on their parameters, then does some calculations and returns the result. I also need to update the rows that were selected. I can only think of ways to do both of these actions with separate queries, but is there a way to do both at once?
Example queries would be:
SELECT SUM(a.val1*b.val1)
FROM a, b
WHERE a.val2 = condition1 AND b.val2 = condition2;
UPDATE a
SET a.val3 = a.val1*b.val1
FROM a INNER JOIN b ON a.val2 = condition1 AND b.val2 = condition2;
Is there a way to combine them?

No. There is no syntax in SQL that allows you to retrieve values and update them in the same query. Use SELECT to retrieve values and UPDATE to change them.
You can use SELECT inside an UPDATE statement as part of the calucalation, but the result will not be the values of what has been updated, only the number of rows that were updated.
Your SELECT statement has a small mistake in it and should be as follows:
SELECT SUM(a.val1*b.val1) FROM a, b WHERE a.val2 = 1 and b.val2 = 1;
This only returns one row: the sum of the product of val1 columns in tables a and b where the val2 columns meet certain conditions.
It is not clear what you are trying to achieve by updating table a with the result of this. If you are looking to set val3 in table a with the product of val1 in tables a and b if the val2 in those tables meet certain criteria, the following might work, but you need to add a join between columns in both tables otherwise val3 will be set to the product of val1 in table a and all the val1 values in table b, which may not be what you want.
UPDATE a
SET a.val3 =
(
SELECT a.val1*b.val1
FROM b
WHERE b.key = a.key
AND b.val2 = condition2
)
WHERE a.val2 = condition1;

Nope :)
In SQL it is always different queries. You can write a function that will do 2 actions, but never one query.

This will have to be done in two steps - Select and Aggregate then Update.

Related

Selecting and storing values, using those values in IN clause

Is there a way to select a set of values from one table and then use those values in an IN clause?
I want to select IDs from one table and then update data for those IDs in another table.
So something like
<some var> = SELECT id from tableA WHERE <something>;
INSERT INTO tableB <stuff> where id IN (<some var>);
I release the variable syntax isn't real. just want to display my intent. I have read about SET a little but am still new to MySQL so it doesnt make perfect sense. Also it mentioned that SET could only set variables of certain types and they all seemed to simple.
Thanks!
You can use insert . . . select:
INSERT INTO tableB (id)
SELECT id
FROM tableA
WHERE <something>;
I'm not sure what IN has to do with this.
EDIT:
Oh, you want an update:
update tableb b join
tablea a
on b.id = a.id
set b.col = ??
where <conditions on a>;
You can also do this using in:
update tableb b
set col = ??
where b.id in (select a.id from tablea a where <conditions>);
The biggest difference is whether or not you want to use information from tablea. If you do, then you need the join version.

Count with a subselect yielding double the amount

I'm new to SQL.
Problem: Say if I were to count the amount that is contained in the alias table of "x" COUNT(x.xValue) to be 217. Now when I add the sub-query "y" and then do the count again, I have the COUNT(x.xValue) to suddenly square its self -> 47089. Why is this happening?
(Note: Both alias tables "x" and "y" have the same amount -> 217.)
How do I fix this problem. I don't want to use Variables or Views.
SELECT COUNT(x.xValue) + COUNT(y.yValue) AS CountXY
FROM
(SELECT value AS xValue FROM table1
WHERE
...) AS x,
(SELECT value AS yValue FROM table1
WHERE
...) AS y
Result of 'CountXY' : 94178.
Result I'm expecting 'CountXY' : 434
The problem is that you are doing two sub-queries and then trying to call the values return directly.
This will behave as selecting one value from table x and matching it to every single value in table y. This obviously creates the squared return effect.
What you need to use is the JOIN to combine both data-sets so that you get the 1 to 1 relationship you are trying to achieve.
This is how the above should be done with your previous sub-query:
SELECT COUNT(A.value) AS x, COUNT(B.value) AS y
FROM table1 AS A
JOIN table1 AS B
ON A.attr1 = B.attr1
AND A.attr2 = B.attr2
WHERE B.attr1 != 'whatever'
AND B.attr2 = 'whatever'
AND A.attr3 = 'something'
AND B.attr3 = 'something different'
The above query should return the correct 1 to 1 relationship you are looking for. Replacing your sub-query with the one above should give you the correct answer

Issue with the TOP 1 query

Is it possible to achieve next thing without using views, but just one single query? I have two tables:
TableA->TanbleB (1-many) ON TableA.Id = TableB.TableAId
I need to update one field in Table A (TableA.Field1) for records in TableA that satisfy condition on one field in tableA (WHERE TableA.Field2=SomeValue)
.
TableA.Field1 will be updated from TableB with value that is last inserted (last inserted value in related records to TableA).
I will put an example:
UPDATE TableA a SET Field1 = (SELECT TOP 1 b.Feild1 * b.Field2 FROM TableB b WHERE b.TableAId = a.id) WHERE field2 = 1
I know Above example doesn't work, but I have many ways tried using INNER JOIN and failed. I had an idea to use something like this:
UPDATE TableA INNDER JOIN ( SELECT ... FROM TABLE B) ON TABLEA.Id= TableB.TableAId SET ....
But the 2ns query should return 1 record for each DISTINCT TableAId, but only the last inserted.
I hope I am making some sense here.
Thanks in advance.
Here is some SQL that will do what you want
UPDATE T1 INNER JOIN T2 ON T1.ID = T2.T1ID SET T1.F2 = [T2].[F2]*[T2].[F3] WHERE (((T1.F1)="ABC") AND ((T2.ID)=DMax("[ID]","[T2]","[T1ID]=" & [T1].[ID])));
This predicated on T1.ID being the primary key for T1 and T2.T1ID being a index field in T2
One of the flaws in Access is that you can't run an "UPDATE" query based on a "SELECT" query, it will usually give the error:
Operation must use an updateable query
The only way around is as you say to create a view of the "SELECT" query and then inner join this on your table, Access is then working with a static recordset and can handle the "UPDATE" query ok
Alternatively you could write a VBA procedure to step through line by line with the Recordset.
Best of luck : )
UPDATE:
SELECT b.TableAId, b.Feild1 * b.Field2 INTO tblView FROM TableB As b WHERE b.field2 = 1

Select up to 3 different fields in a table based on the value in another table?

Ive seen plenty of examples but none seem to get me where I need it.
I want to Select 1 field from table A, then Check table B for a value, if that value is true(boolean) then also Select field 2 and 3 from table A to return all 3 fields. So if value from B is false only 1 field is selected from table A, if true, all 3 are selected.
Select Field1
, Case
When Exists (
Select 1
From TableB
Where SomeField = 1
And ...
) Then TableA.Field2
Else Null
End As Field2
, Case
When Exists (
Select 1
From TableB
Where SomeField = 1
And ...
) Then TableA.Field3
Else Null
End As Field3
From TableA
Update
The above solution works fine if what you want is a three column result every time even if some of those columns are null. However, if what you want is a different number of columns returned based on the query, then this is something that cannot be done in a single query. The SQL language was not geared to handle on-the-fly schema generation. My suggestion would first be to evaluate why you want a different number of columns from the same query and determine if you cannot simply handle the scenario in your middle-tier where column 2 or 3 is NULL. That is by far the simplest solution and could be done in a single query:
Select TableA.Field1, TableA.Field2, TableA.Field3
, (
Select TableB.SomeBooleanColumn
From TableB
) As TableBValue
From TableA
Your middle-tier code would then determine whether to do something with Field2 and Field3.
That said, if you insist on having two column structures, you need two queries:
Select TableA.Field1
From TableA
Where Exists (
Select 1
From TableB
Where TableB.SomeColumn = 0
)
After calling this query, you would evaluate whether you got a row. If you got no rows, you could then call this query:
Select TableA.Field1, TableA.Field2, TableA.Field3
From TableA
Where Exists (
Select 1
From TableB
Where TableB.SomeColumn = 1
)
What hasn't be stated in the OP is the scenario where there is no row in TableB.
Use case expressions:
select A.field1,
case B.boolValue when 1 then A.field2 end AS field2,
case B.boolValue when 1 then A.field3 end AS field3
from TableA A join TableB B on A.? = B.?
I don't know what your A and B tables are linked on so you'll have to fill in the ? marks. This will return null values for field2 and field3 when B.boolValue is false.

MySQL sub query select statement inside Update query

I have 2 tables: tbl_taxclasses, tbl_taxclasses_regions
This is a one to many relationship, where the main record ID is classid.
I have a column inside the first table called regionscount
So, I create a Tax Class, in table 1. Then I add regions/states in table 2, assigning the classid to each region.
I perform a SELECT statement to count the regions with that same classid, and then I perform an UPDATE statement on tbl_taxclasses with that number. I update the regionscount column.
This means I'm writing 2 queries. Which is fine, but I was wondering if there was a way to do a SELECT statement inside the UPDATE statement, like this:
UPDATE `tbl_taxclasses` SET `regionscount` = [SELECT COUNT(regionsid) FROM `tbl_taxclasses_regions` WHERE classid = 1] WHERE classid = 1
I'm reaching here, since I'm not sure how robust MySQL is, but I do have the latest version, as of today. (5.5.15)
You could use a non-correlated subquery to do the work for you:
UPDATE
tbl_taxclasses c
INNER JOIN (
SELECT
COUNT(regionsid) AS n
FROM
tbl_taxclasses_regions
GROUP BY
classid
) r USING(classid)
SET
c.regionscount = r.n
WHERE
c.classid = 1
Turns out I was actually guessing right.
This works:
UPDATE `tbl_taxclasses`
SET `regionscount` = (
SELECT COUNT(regionsid) AS `num`
FROM `tbl_taxclasses_regions`
WHERE classid = 1)
WHERE classid = 1 LIMIT 1
I just needed to replace my brackets [] with parenthesis ().