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
Related
In my application I keep track of users' total unread notifications together with other user data in user table. Notifications are in separate table, notifications of similar nature are stacked into a single row, each row tracks count and last timestamp. Now I want to implement a function of marking all notifications as read until a particular timestamp. I made this query using mathematical subtraction between two columns from two different tables:
UPDATE
core_notification n
LEFT JOIN
core_user u
ON
n.notification__user_id = u.user__id
SET
n.notification__if_read = 1,
u.user__notification_unread_count = u.user__notification_unread_count - n.notification__main_count
WHERE
n.notification__timestamp <= 123456 AND
n.notification__if_read = 0 AND
n.notification__user_id = 123;
Problem: lets say in user table "user__notification_unread_count" has a value of 4 and there are two notifications which both have a "notification__main_count" value of 2. My goal after running this query and updating both notifications as read is to have "user__notification_unread_count" value of 0 (4 - 2 - 2 = 0). However the result is always 2. I tried various join methods but it seems that it is just how databases work when updating multiple rows (each updated row overwrites previous update completely, but that is only my assumption).
Is there a way I could achieve the result I want with a single query?
This is a situation where you are probably better off using a trigger. But . . . you can do it in a single update, just with more work:
UPDATE core_notification n LEFT JOIN
core_user u
ON n.notification__user_id = u.user__id LEFT JOIN
(SELECT n2.notification__user_id,
SUM(n2.notification__main_count) as total_notification__main_count
FROM core_notification n2
WHERE n2.notification__timestamp <= 123456 AND
n2.notification__if_read = 0 AND
n2.notification__user_id = 123;
GROUP BY n2.notification__user_id
) n2
ON n2.notification__user_id = n2.notification__user_id
SET n.notification__if_read = 1,
u.user__notification_unread_count = u.user__notification_unread_count - n2.total_notification__main_count
WHERE n.notification__timestamp <= 123456 AND
n.notification__if_read = 0 AND
n.notification__user_id = 123;
The n2 derived table separately calculates the total that needs to be subtracted.
I am fairly new to ms access (working with access 2013) and unfortunately am stuck with a problem.
I am currently working on an update query with 2 tables. In table 1 I would like to update all fields of a column with a "1" based on multiple criteria. Three different criteria exist in both tables. I only want to update the column if 2 of the criteria are exactly the same in both tables and one criteria is larger in table 2 than in table 1. However, unfortunately even if all criteria match, that does not mean that the certain case is unique. However, I just want to Update a "1" only once per unique row of table 2.
So basically, I have to questions:
Is the current code correct concerning the match I want to make?
Is there any way to tell access to only update once per unique row in table 2?
Thanks a lot for your help!
This is my current code:
UPDATE Table2 LEFT JOIN [Table1] ON (Table2.Criteria1 = [Table1].Criteria1) AND (Table2.[Criteria2] = [Table1].[Criteria2]) SET [Table1].Column = 1
WHERE (((Table2.[Criteria1])=[Table1].[Criteria1]) AND ((Table2.Criteria2)=[Table1].[Criteria2]) AND ((Table2.Criteria3)>=[Table1].[Criteria3]));
A left join and a where on same table work as an inner join. Looking to your code seems you need a join between table1 and table2 for update table2. So the syntax and the condition should be:
UPDATE Table2
SET [Table1].Column = 1
FROM Table2
INNER JOIN [Table1] ON Table2.Criteria1 = [Table1].Criteria1
AND Table2.[Criteria2] = [Table1].[Criteria2]
AND Table2.Criteria3>=[Table1].[Criteria3]
But if you need an update only for a single rows the you could trying using the min(id) resulting from the matching row:
UPDATE Table1
SET [Table1].Column = 1
WHERE Table1.ID = (
SELECT MIN(ID)
FROM Table2
INNER JOIN [Table1] ON Table2.Criteria1 = [Table1].Criteria1
AND Table2.[Criteria2] = [Table1].[Criteria2]
AND Table2.Criteria3>=[Table1].[Criteria3]
)
I've created several tables in a test database based on columns/rows in my main database.
test.websites.url = main.websites.url
test.category_main = main.websites.category1
test.category_01 = main.websites.category2
test.category_02 = main.websites.category3
etc...
The test database columns already contain all the rows from the main
database, but I need to add the rows from the respective tables to the
category_to_website table and create foreign keys because there is
currently no relation between them in the test database. That is why I have joined the
main database in the query.
When trying to use the main table as a reference for updating the existing rows in the test database, some values are updated but they are not always correct. I'm executing the query from the test database.
My query:
UPDATE category_to_website
LEFT JOIN main.websites
ON websites.url = main.websites.url
LEFT JOIN category_01
ON category_01.name = main.websites.category2
SET category_to_website.category_01_id = category_01.id
WHERE category_to_website.category_01_id = main.websites.category2
My database schema:
I suspect that the issue is with the type of JOINs I am doing, but I've tried LEFT JOIN, JOIN, and INNER JOIN and get the same results. I think that maybe I need a SELECT sub query or my WHERE clause is off?
EDIT
Based on the comments I was able to get this all sorted out. Here are the steps I took.
1. Merged the category_* tables into a category table.
2. Joined the test.websites table into the query.
UPDATE test.category_to_website
LEFT JOIN test.websites
ON test.websites.id = category_to_website.url_id
RIGHT JOIN main.websites
ON test.websites.url = main.websites.url
INNER JOIN test.category
ON test.category.name = main.websites.category1
SET category_to_website.category01_id = category.id
WHERE category_to_website.url_id = test.websites.id
UPDATE category_to_website
JOIN websites
ON websites.id = category_to_website.url_id
JOIN main.websites
ON websites.url = main.websites.url
JOIN category
ON category.name = main.websites.category1
SET category_to_website.category01_id = category.id
SETs are done to rows that participate in the JOINs. But if you LEFT JOIN to category_to_website then all its rows participate so then you must restrict them in a WHERE the way you already did in the ON.
Thank goodness you have started to relationalize that horrible schema. Keep going: replace all multiple categery_ columns in each table by just one category column for id or name. And if you named them category_id and category_name their nature would be clear. (Maybe post your next version as a new question.)
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
I have read other posts and I really don't understand what I am doing wrong here because it is such a simple statement.
anything in '()' are comments
Query:
[UPDATE users, sites
SET users.idSiteRegUsers='1'
WHERE sites.SiteActivateSites='DEL' ]
(TBL's to select to update within the whole query)
(Setting the users tbl idSiteRegUsers to 1)
(Where only the sites in tbl sites = DEL)
I've also read http://bugs.mysql.com/bug.php?id=52651 and tried changing the INT to VARCHAR and UPDATING 0 to DEL due to the bug but still same result.
Issue:
I have 2129 records that need updating as found using a simple select statement to understand the number of results.
([SELECT
sites.SiteActivateSites,
sites.id_Sites,
users.idSiteRegUsers,
users.CompanyNameUsers,
sites.LinkNameSites
FROM
users
INNER JOIN sites ON users.idSiteRegUsers = sites.id_Sites
WHERE
sites.SiteActivateSites != '1']) 'simple'
But the UPDATE query updates all 4000+ records, not just the records that = DEL but the ones that are reference to another value e.g. = 1.
Have I missed something?
Cheers,
Joe
Just as with your SELECT command, you need to tell MySQL how the tables are joined: your UPDATE is currently doing a full cartesian product of both tables (with every row in users joined with every row in sites - therefore the WHERE condition filtering on sites is still resulting in a match on every record from users).
Try instead:
UPDATE users JOIN sites ON users.idSiteRegUsers = sites.id_Sites
SET users.idSiteRegUsers='1'
WHERE sites.SiteActivateSites='DEL'