I'm using MySQL 5.1.41 on ubuntu 12.10 and MySQL Workbench.
I have 2 product tables, t1 and t2. t1 is the live data and t2 is a imported data ready to be updated into t1 to update all the new product prices. So I run:
SELECT * FROM t1
JOIN t2 ON t1.id = t2.id
WHERE t1.price != t2.price;
This returns 1201 records where the price is different and needs to be updated. So I run:
UPDATE t1 JOIN t2 ON t1.id = t2.id
SET t1.price = t2.price
WHERE t1.price != t2.price;
This completes without error and reports 1143 row(s) affected, Rows matched: 1143 Changed: 1143 Warnings: 0
So already something here is not right. 1201 records were different in the select query, but only 1143 changed using the same join and criteria?
Running the initial select query I'd expect to see 58 records that still had different prices. But when running it I get the same 1201 as I did initially. It's as if the updates are not being committed.
Any ideas?
The number (1201) that your SELECT shows is not records of t1 but rows from the JOIN of two tables. If the two id are not both UNIQUE or PRIMARY KEYs then this is expected. Some rows of t1 match multiple rows from t2. But when the UPDATE is done they are only updated once (this is a MySQL "feature" or "bug" of UPDATE that checks WHERE conditions sequentially during an update statement.
Try this to see how many rows (of t1) should be updated:
SELECT * FROM t1
WHERE EXISTS
( SELECT *
FROM t2
WHERE t1.id = t2.id
AND t1.price != t2.price
);
Related
I need to process a queue and MySQL for update is the goal for that.
Everything is working fine, except the fact for update is locking joined tables too.
I have 4 functions that are called simultaneously and 2 of them joins the same table.
The problem is that it seems it's locking all joined table and not specific joined rows.
My code is something like that (just example):
Query #1:
SELECT t1.id, t1.name, t2.balance FROM t1
LEFT JOIN t2 ON t2.user_id = t1.id
WHERE t2.balance < 0
LIMIT 10
FOR UPDATE
SKIP LOCKED
Query #2:
SELECT t3.id, t3.action, t2.balance FROM t3
LEFT JOIN t2 ON t2.user_id = t3.user_id
WHERE t2.balance < 0
LIMIT 10
FOR UPDATE
SKIP LOCKED
I didn't find anything related to this.
Is there a way to avoid that behavior?
Im looking to compare two different tables row by row to ensure that both the file name and the count associated with the filename match. If either does not match I want to output the two rows that are not matching.
I am Using MySQL as my database for the operation
For example
Expected Actual
FileName Count FileName Count
name1.txt 4 name1.txt 4
name2.txt 7 name2.txt 7
name3.txt 4 name4.txt 4 (invalid filename)
name5.txt 4 name5.txt 5 (invalid count)
Output:
The fOllowing rows did not match:
Expected: Actual:
name3.txt 4 name4.txt 4 (because of filename)
name5.txt 4 name5.txt 5 (because counts are diff)
The purposes of this is for a validation script. There are two tables, one for the expected results, and one for the actual results. The sql is to compare the rows in each table to make sure that they are identical. There are 4 columns in each table. For the tables to be considered "identical" the filename and the count associated to that filename must be found in both tables. If that doesn't happen I would like to output the rows that were not able to find a match.
You want NOT EXISTS :
Table1 :
SELECT t1.*
FROM table1 t1
WHERE NOT EXISTS (SELECT 1 FROM table2 t2 WHERE t2.filename = t1.filename AND t1.count = t2.count);
Table2 :
SELECT t2.*
FROM table2 t2
WHERE NOT EXISTS (SELECT 1 FROM table1 t1 WHERE t2.filename = t1.filename AND t1.count = t2.count);
If you want to collate them as one result set then use union all :
SELECT t1.filename, t1.count, 'table1' as table_name
FROM table1 t1
WHERE NOT EXISTS (SELECT 1 FROM table2 t2 WHERE t2.filename = t1.filename AND t1.count = t2.count)
UNION ALL
SELECT t2.filename, t2.count, 'table2'
FROM table2 t2
WHERE NOT EXISTS (SELECT 1 FROM table1 t1 WHERE t2.filename = t1.filename AND t1.count = t2.count);
i have two tables named t1 and t2.
t1 have 500 rows and t2 have 220000 rows. the relationship between these tables are t1.t_id and t2.t1_t_id. not all t1 rows have corresponding row in t2.
now this line of code works fine
SELECT t1.t_id , t2.t1_t_id FROM t1, t2
where t2.t1_t_id = t1.t_id and t1.t_id=11
but this does not work
SELECT t1.t_id , t2.t1_t_id FROM t1, t2
where t2.t1_t_id = t1.t_id
in case picture not available
error is:
1 error were found during analysis
1. missing expression (near "ON" at position 25)
SQL query edit
set foreign_key_checks=on
mysql said
2006 server has gone away
what i want to do after solving this issue is:
t1 have a column named p_id which is primary.
i recently added a new column in t2 named t1_p_id.
now i want to update t2.t1_p_id = t1.p_id where t2.t1_t_id = t1.t_id.
i know i should use inner join on t2.t1_t_id = t1.t_id in a update query but it also issues the same error.
my guess for the error is that i should specify some specific condition because not all t1 have corresponding row in t2
update
i am querying by browser by mysql console
thanks
I have two tables. t1 contains business info and t2 contains business hours.
I use the following query to return results where data is present in t1 and is not in t2.
SELECT DISTINCT(t1.id)
t1 LEFT JOIN t2 ON (t1.id = t2.busId)
WHERE t2.busId IS NULL
This query works, but with about 15K records in t1 and 50K rows in t2 this literally takes minutes to return results. I wonder if it's possible to speed it up.
I've tried lots of versions of this update query using answers from previous questions but can't seem to get it to work. The latest one I have returns 0 rows affected.
I get 2017 results when I run this query (which is what I want)
SELECT *
FROM table_1 t1
INNER JOIN table_2 t2 ON t1.company = t2.company
WHERE t1.user = 123 AND t2.group_id = 3
But I want to run an update query like this
UPDATE table_1 AS t1
INNER JOIN table_2 as t2
ON t1.company = t2.company
SET t1.user = t2.user
WHERE t1.user = 123 AND t2.group_id = 3
But I get 0 rows affected
Why does this update query not update the 2017 records that are returned in query 1?
I expect your select query is returning too many rows, as you have an error:
INNER JOIN table_2 t2 ON t2.company = t2.company
I think you want:
INNER JOIN table_2 t2 ON t1.company = t2.company
Are you sure that the table_1.user and table_2.user values are actually different before you run the update query? MySQL will report the number of rows that were actually changed, not the number of rows that were selected and examined.
Try running this query first, to see how many of your 2017 rows actually need updating:
SELECT *
FROM table_1 t1
INNER JOIN table_2 t2 ON t1.company = t2.company
WHERE t1.user = 123 AND t2.group_id = 3
AND t1.user <> t2.user
Update
It turns out that this can happen if the data types are similar enough to be compared, but not enough that the values in table_2.user can be assigned to table_1.user. In that case, MySQL will issue a warning ("Out of range value adjusted for column 'user' at row xxx"), but will otherwise allow the query to succeed. Running the query again will show that the values are still different, but won't change what is actually stored in t1.user.
If you modify the update query to only match the rows that need to be changed, and run this from the command line, then MySQL will tell you exactly what's going on:
mysql> UPDATE table_1 AS t1
-> INNER JOIN table_2 as t2
-> ON t1.company = t2.company
-> SET t1.user = t2.user
-> WHERE t1.user = 123 AND t2.group_id = 3
-> AND t1.user <> t2.user
Query OK, 0 rows affected, 2017 warnings (0.01 sec)
Rows matched: 2017 Changed: 0 Warnings: 2017
Rows matched: 2017 indicates that there were 2017 rows that need to be updated (if the values were already correct, this would be 0)
Changed: 0 means that none of them actually ended up with different values, despite the SET t1.user = t2.user command.
Warnings: 2017 says that something went wrong, and you should issue a SHOW WARNINGS query to find out what it was. In this case, the disparity between what can be stored in a smallint vs an integer meant that the data was being truncated in table_1.