I checked the recommended threads before posting as well as tried rewriting this query a few times, however from my testing on Microsoft Sql Server this query is the most optimized for what I am trying to achieve.
Background: I am converting from Microsoft SQL Server to MySQL. This query originally was optimized on SQL Server, I am trying to rewrite it for MySQL.
SELECT (T1.`ID`, T1.`Col1`, T1.`Col2`,
T2.`ID` as 'Data1', T2.`Col7`, T2.`Col5`,
T2.`Col2` as 'Data2', T2.`Col8`, T2.`Col6`,
T2.`Col9`, T2.`Col10`,
T3.`Col3` as 'Data3',
T4.`Col3` as 'Data4', T4.`Value`) FROM
(
(SELECT `ID`, `Col1`, `Col2` FROM TABLE1) T1
FULL OUTER JOIN
(SELECT `ID`, `Col4`, `Col7`, `Col5`, `Col2`, `Col8`, `Col6`, `Col9`, `Col10` FROM TABLE2) T2 on T2.`Col4` = T1.`ID`
FULL OUTER JOIN
(SELECT `Col12`, `Col3` FROM TABLE3) T3 on T3.`Col12` = T2.`ID`
FULL OUTER JOIN
(SELECT `Col12`, `Col3`, `Value` FROM TABLE4) T4 on T4.`Col12` = T2.`ID`
);
This query pulls all the associated data that I need. I have implied a lot of the columns (removed some) and tried to make it forward without my table names and column names. But the error I receive is
"SELECT" is not valid at this position for this server version, expecting '(', WITH
MySQ 8.0.21
Related
Ive got a big SQL statement.
But i want to test the where clause on the firsttable (T1) and after that, make all the joins on the rows selected using the where clause.
Because actually the query is very slow, cause MySQL join all the tables to t1 and then test the where clause !
SELECT * from FIRSTTABLE T1
LEFT JOIN T2 on (....)
LEFT JOIN T3 on (....)
WHERE T1.column = '1' OR T1.column= '5'
Any ideas ?
You can do something like:
SELECT * from
(
SELECT * from FIRSTTABLE T1
WHERE T1.column = '1' OR T1.column= '5'
) as T2
LEFT JOIN T3 on (....)
LEFT JOIN T4 on (....)
This is too long for a comment.
SQL queries are compiled and optimized before they are executed. The order of clauses in a query really has nothing to do with the final execution plan. More specifically, the filtering conditions in the WHERE clause could take place before, after, or even during the JOIN processing.
You can start to learn about SQL optimization by understanding indexes. The MySQL documentation is a very reasonable place to start.
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 want to create a view in mysql.But that in mysql does't support subquery.
How to write the sql without subquery?
select * from dev_location t1
inner join
(
select
`dev_location`.`device_id` AS `device_id`,
max(`dev_location`.`id`) AS `id`
from
`dev_location`
group by `dev_location`.`device_id`) t2
on t1.id = t2.id
MySQL views don't support subqueries in the from clause. The following should work in a view:
select dl.*
from dev_location dl
where not exists (select 1
from dev_location dl2
where dl2.device_id = dl.device_id and
dl2.id > dl.id
);
This reformulates the query to say: "Get me all the rows from dev_location where the device_id has no greater id." This is an awkward way of getting the max.
And, with an index on dev_location(device_id, id), it might perform better than your version.
Am trying to get the all rows from Tabl1 which are not available in Table2 with help of NOT IN MSQL query. But am getting timeout exception and query is not getting executed. Below is the mysql query which I am using.
SELECT * FROM identity WHERE
unique_id NOT IN (SELECT Message_Queue.index FROM Message_Queue);
Could any please tell the reason or any other way for replacement of NOT IN operation?
When you have so many records in the in() clause then you should use a join instead
SELECT t1.*
FROM table1 t1
left join table2 t2 on t2.myId = t1.myId
where t2.myId is null
Because in MySQL NOT IN is less performant, try using EXISTS
SELECT *
FROM identity a
WHERE NOT EXISTS
(
SELECT null
FROM Message_Queue b
WHERE b.index = a.unique_id
);
you should also put an index on those columns.
GOT error for the following query in MYSQL(version5.1)
SELECT year,month,sum(fact_1),sum(fact_2),sum(fact_3),sum(fact_4)
from(
select year,month,fact_1,fact_2,0 as fact_3,0 as fact_4 from table_1
intersect
select year,month,0 as fact_1,0 as fact_2,fact_3,fact_4 from table_2
) as combined_table
group by month,year
Error Line with code#1064:-
You have an error in your SQL syntax;
check the manual that corresponds to
your MySQL server version for the
right syntax to use near 'select
year,month,0 as fact_1,0 as
fact_2,fact_3,fact_4 from table_2 ) as
ct g' at line 5
but following query was giving desired Result:-
SELECT year,month,sum(fact_1),sum(fact_2),sum(fact_3),sum(fact_4)
from(
select year,month,fact_1 ,fact_2,0 as fact_3,0 as fact_4 from table_1
union
select year,month,0 as fact_1,0 as fact_2,fact_3,fact_4 from table_2
) as ct
group by month,year
Can anybody tell what error i am committing?
can Anybody help me to understand the root cause behind the Problem.
you can fake INTERSECT quite easily using an INNER (self) JOIN, this way you’ll only get rows from both resultsets:
SELECT `a`.`id`, `a`.`name`
FROM `a`
INNER JOIN `b`
USING (`id`, `name`)
MINUS can be faked with a LEFT JOIN:
SELECT DISTINCT `a`.`id`, `a`.`name`
FROM `a`
LEFT JOIN `b`
USING (`id`, `name`)
WHERE `b`.`id` IS NULL
MySQL doesn't support the INTERSECT keyword. Full syntax of SELECT for 5.1 is here:
http://dev.mysql.com/doc/refman/5.1/en/select.html