I got one table called family, which contains a column called power. I want to update maximum ten values of power by adding one in each row and the rest remains the same. I try my own method by creating another table which contains the maximum ten values that I want to update and create a query below, but got some problems. Here's the query:
UPDATE family
SET family.total = (SELECT totalmax.total FROM totalmax
INNER JOIN familyone
ON family.family_id2 = totalmax.family_id2
WHERE family.family_id2 = totalmax.family_id2)
Can someone tell me where's the problem with this query and is there any other methods to solve my problem?
You could do this with a join
UPDATE family
INNER JOIN
totalmax
ON family.family_id2 = totalmax.family_id2
SET family.total = totalmax.total
Related
There are many varied posts about this matter, but I am unable to find the answer I need. I am hoping this question is unique.
I am trying to append all the data from one table to another, without creating new records. The data in the second table is really a subset of data for a portion of the existing records in the first table.
For example:
I have the table "SPK". And I want to write all of the data from SPK into the table "RCT". The common field between each record I want to match is the RegID, which is unique in both tables (i.e. there is only one SPK record per RCT record).
If I understand correctly, you mean append the columns in one table (call it SECOND) to the other (call it FIRST).
In that case, does this work ?
UPDATE
regcontactsTest
JOIN
speakersTest
ON speakersTest.RegistrationID = regcontactsTest.RegistrationID
SET regcontactsTest.presentationtitle = speakersTest.presentationtitle
EDIT: Updated the query based on Mariadb syntax
You need to use JOIN. For general Update join :
update tab1 a
join tab2 b ON a.join_colA = b.join_colB
SET a.columnToUpdate = [something]
Or in other words:
update
tab1 a
join tab2 b on ..
set a.field=...;
I am using MySQL for the following: I have 2 tables, sw_products and sw_components. Each sw_product can be made up of multiple sw_components. And there is another relation table where each row specifies the product and a component it is uses.
Each sw_component has a status: 'ready', 'not-ready', and 'unusable'. And each sw_product also has a status which corresponds to the lowest status of all of its components.
What I was doing was trying was to create a TRIGGER UPDATE when sw_components updates, but then I'd need to cycle through all the sw_products that use that sw_component and change their status if the new status is higher. How can I do this inside a trigger? (assign a variable to a select statement, and cycle through each row, maybe?)
Thanks!
I´m the OP. I was able to solve the problem after some thinking. As Ultimater said, you can use cursors, but they´re read only, so you can go into some trouble, and I thought they were over-complicated (but still, thanks!).
What I did was changed the status from VARCHARS to INTEGERS, then I created a View of each product alongside their lowest status (using a MIN(status):
CREATE VIEW product_status_by_component AS
SELECT s_name, s_version, min(status) AS min
FROM sw_components
JOIN sw_build
ON sw_components.name = c_name AND sw_components.version = c_version
GROUP BY s_name, s_version;
Then I used a trigger each time a components status got updated. I found out you Update unrelated tables even if the trigger does not belong to them, and that you could join tables inside an update:
UPDATE sw_products a
LEFT JOIN product_status_by_component b ON
a.name = b.s_name AND a.version = b.s_version
SET
a.status = b.min;
Well I m pretty stuck in this problem, I have two tables with identical structure, I want to update first table with values of 2nd table. I have following query but mysql is throwing the error.
UPDATE property p
SET ROW = (SELECT * FROM temp_property t WHERE p.id= t.id)
Can anybody shed some light on this?
I'm pretty sure you can't update an entire row all at once. You need to specify the column names.
UPDATE property p, temp_property t
SET p.col1 = t.col1, etc
WHERE p.id=tp.id
(Fixed query for MySQL.)
I am brand new to SQL and am working with the following code provided to us by one of our vendors:
SELECT DISTINCT MriPatients.PatientID
INTO #UniquePt
FROM MriPatients
INNER JOIN #TotalPopulation ON MriPatients.PatientID = #TotalPopulation.PatientID
Set #TotalUniquePatients = (Select Count(*) FROM #UniquePt)
What happens is the Set line causes #TotalUniquePatients to be set to 0 even though there are many unique patient ids in our database. That value is then later used as a denominator in a division which causes a divide by 0 error.
Now it seems to me that this is easy to fix by using COUNT DISTINCT on the MriPatients table; then you don't need to create #UniquePt at all...this is the only place that table is used. But, I don't understand why the code as it is gets a 0 result when counting #UniquePt. If you remove the INNER JOIN, the Set returns a correct result...so what does the INNER JOIN do to #UniquePt?
If it matters, we are using SQL Server 2008.
The result is 0 because of 1 of 2 situations:
#TotalPopulation is empty
#TotalPopulation contains no records that have the same value for PatientID as the records in MriPatients
How are you populating #TotalPopulation?
A COUNT DISTINCT won't necessarily do the same thing. It depends on what you fill #TotalPopulation with. If all you want is the number of unique patients in MriPatients then yes, the COUNT DISTINCT will work. But if you're filling #TotalPopulation based on some kind of logic then they're the COUNT DISTINCT won't necessarily give you the same results as the COUNT of the joined tables.
The INNER JOIN causes you to insert ONLY records that have a matching PatientID in the #TotalPopulation table.
I'm guessing you don't, or that table isn't populated, which is causing the issue.
Is there a reason you are joining to it in the first place?
I am attempting to do an UPDATE with a JOIN. I have two tables:
player_tracking has a list of all
players that each user has added to
tracking.
users is the user list. each user
can set fsp_f to 1 or 0.
I want to update all rows in player_tracking for users who have fsp_f set to 1. Here is my example code:
UPDATE player_tracking AS pt
LEFT JOIN users AS u ON u.name = pt.user
SET pt.newtome = pt.newtome - 1
WHERE pt.first = 'Brett'
AND pt.last = 'Gardner'
AND pt.sport = 'mlb'
AND u.fsp_f = 1
The problem is that there are 22 rows to update, yet the UPDATE query only affects 2. Why? Is my query wrong?
Here is the data found in player_tracking pertaining to "Brett" "Gardner" "mlb":
http://pastebin.com/kyf8SCy8
i believe that if you change the LEFT JOIN to JOIN you will see the exact rows that get updated since you are using a field form users in the WHERE part of the statement.
so basically you are trying to check if u.fsp_f = 1 when there could be rows that do not join users and therefore will have the value as NULL.
Additionally it seems that the general layout of your query is not correct either, since you are joining on the SET statement and not in the UPDATE part - where you instructed which table to update