The below same query is returning different results for MySQL 5.6 and MySQL 5.7
SELECT T1.ID, T1.NAME
FROM T1
JOIN T2 ON T1.ID = T2.S_ID
JOIN T3 ON T2.R_ID = T3.DR_ID
WHERE
T2.START_DATE <= '2022-10-01'
T2.END_DATE > '2023-10-01'
AND T3.DT_ID = 2
ORDER BY T1.ID
I expect the above query to return same results in MySQL 5.6 and MySQL 5.7 databases which have the same data.
Unfortunately, MySQL 5.7 is returning more records which is wrong, but if I remove ORDER BY T1.ID for MySQL 5.7 it has the same results as what I get using MySQL 5.6 (with ORDER BY) and the result is correct.
Related
How to return deleted records of following query in MySQL?
DELETE t1
FROM t1
LEFT JOIN t2 on (t1.t2_id = t2.id)
WHERE t2.id IS NULL OR t2.is_valid = false
Background:
$ mysql --version
mysql Ver 14.14 Distrib 5.6.23, for osx10.8 (x86_64) using EditLine wrapper
MySQL doesn't have the equivalent of the output or returning clauses provided by other databases. Your best bet is a temporary table:
CREATE TABLE TheDeletedIds as
SELECT t1.id
FROM t1 LEFT JOIN
t2
ON t1.t2_id = t2.id
WHERE t2.id IS NULL OR t2.is_valid = false;
DELETE t1
FROM t1
WHERE t1.id IN (SELECT id FROM TheDeletedIds);
Then the table you just created has the ids you want.
Note: It is important to use the newly-created table for the deletion. Otherwise, another thread/process could change the data between the time you capture the ids and the time you delete them.
try
DELETE t1 OUTPUT DELETED.*
FROM t1
LEFT JOIN t2 on (t1.t2_id = t2.id)
WHERE t2.id IS NULL OR t2.is_valid = false
I'm trying to convert this MySQL query to SQL Server, but I do not know much about SQL Server
SELECT
*
FROM
Table1 AS T1
INNER JOIN Table2 AS T2
ON T1.Column1 = T2.ColumnX
WHERE
T1.ColumnY = 'xxxx'
GROUP BY
T1.Column1
Somebody can help me?
Your query is just an erroneous query, because you are using select * with group by. This query uses a MySQL extension. And, the default settings in more recent versions of MySQL would generate an error.
Here is one method for converting this to SQL Server:
SELECT TOP (1) WITH TIES *
FROM Table1 AS T1 INNER JOIN
Table2 AS T2
ON T1.Column1 = T2.ColumnX
WHERE T1.ColumnY = 'xxxx'
ORDER BY ROW_NUMBER() OVER (PARTITION BY T1.Column1 ORDER BY (SELECT NULL)) ;
Probably a better method (from a performance perspective) uses a lateral join:
SELECT *
FROM Table1 T1 CROSS APPLY
(SELECT TOP (1) T2.*
FROM Table2 T2
WHERE T1.Column1 = T2.ColumnX
) T2
WHERE T1.ColumnY = 'xxxx' ;
Both of these choose arbitrary rows from Table2 when there is more than one match.
I am running a query to remove duplicate entries. It works without the LIMIT statement, but overloads the server. I'd like to batch process it using the LIMIT statement.
DELETE t1
FROM data as t1
join data as t2
WHERE t1.type = t2.type
AND t1.timestamp = t2.timestamp
LIMIT 100
And with the LIMIT statement receive the error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'LIMIT 100' at line 6
you can't use direcly the limit in delete you could use in select
DELETE data
FROM data as t1
join (
select distinct type, timestamp from data limit 100
) t2 on t1.type = t2.type AND t1.timestamp = t2.timestamp
Multi-table DELETE and UPDATE do not permit a LIMIT clause. This is the cause of the error message. To workaround...
To do massive DELETEs (or UPDATEs), loop through the table in chunks. This discusses the details. For efficiency, it uses the PRIMARY KEY so that walking through the table is efficient.
I'm coding queries for a MYSQL database i created. I have a very large amount of rows and i'm asking myself which version of update is faster.
Is faster:
a) Version with WERE
UPDATE table t1 JOIN table t2
SET t1.c1 = 'something'
WHERE t1.c1 = t2.c1
or
b) Version without WERE
UPDATE table t1 JOIN table t2 on t1.c1 = t2.c1
SET t1.c1 = 'something'
?
The question could be also: is it better to specify the "CONDITION" in the JOIN of two tables or in the WERE statement in an update in MYSQL to get faster performance?
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
);