can anybody suggest the Alternate of "intersect" & "Minus" for MYSQL? - mysql

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

Related

cant't run the follorwing Query Clause query shows an error message in goormide

Running a query that has similar syntax:
WITH table1
AS
(SELECT
COUNT(*)
FROM photos
),
table2 AS (
SELECT
COUNT(*)
FROM
users
)
SELECT table1 / table2;
I'm getting the following error, but not sure why? Really new to SQL. Thanks !
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 'table1 AS
You can't use the CTE syntax because you are using MySQL Server 5.7. CTE is a new feature introduced in MySQL 8.0.
But you don't need it for the query you show. You can do it this way:
SELECT table1.count / table2.count
FROM (SELECT COUNT(*) AS count FROM photos) AS table1
CROSS JOIN (SELECT COUNT(*) AS count FROM users) AS table2;

SELECT FROM with multiple Outer Joins on Subquery

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

Sql Join syntax on two tables MySql

I use as RDBMS the version 5.5.24-log of MySql.
With this sql query in output I have all records presents on the two tables tbl_1 and tbl_2:
SELECT * FROM `tbl_1` CB
JOIN `tbl_2` A ON A.xCode = CB.xCode
AND RIGHT (A.xElement, 1) = CB.xElement
WHERE
CB.xType IN ('A')
AND MONTH(xDate) BETWEEN 1 AND 4
ORDER BY xDate DESC;
Now I need extract from tbl_1 all records not presents in tbl_2 and I have tried this sql query but I have error
Lost connection to MySQL server during query
SELECT i.* FROM
`tbl_1` i
LEFT JOIN `tbl_2` o ON o.xCode = i.xCode_cabina
AND RIGHT (o.xElement, 1) = i.xElement
WHERE
i.xType IN ('A')
AND MONTH(xDate) BETWEEN 1 AND 4
AND o.xElement IS NULL ;
Can you please help me figure out the problem?
Thanks in advance.
Select i.* From tbl_1 i
WHERE i.xType in ('A') and
MONTH(i.xDate) bewteen 1 and 4 and
i.xCode_cabina not exist (select o.xCode from tbl_2 o where o.xElement is null);
Are you looking to do something like this?
SELECT * FROM tbl_1
WHERE xCode NOT IN (SELECT xCode FROM tbl_2)
Source: Mysql: Select rows from a table that are not in another

How Can i write update statement in mysql with select query?

I want to update table1 by taking max(date) and min(Date) from Table2 for only if table1.status=100 and table1.Todate='0000-00-00 00:00:00'
same thing I am trying with following query but it is giving error in group by
update table1 s
left join table2 t
on s.stCode=t.tsTask
set s.stActFrom= min(t.tsDate),s.stActTo=max(t.tsDate)
WHERE s.stActTo='0000-00-00 00:00:00' and s.stStatus=100
group by t.`tsTask`
if i execute this query i am getting following error.
#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 'group by t.tsTask' at line 1
Can any one tell me how i can write this update query?
update s
set s.stActFrom= t.MinDate,
s.stActTo = t.MaxDate
FROM table1 s left join
(
SELECT MinDate = min(tsDate), MaxDate = max(tsDate)
FROM table2
group by `tsTask`
) AS t
WHERE s.stActTo='0000-00-00 00:00:00' and s.stStatus=100
ON s.stCode=t.tsTask
I have tried the above answers. That is not given me the required answer. So i have got answer from the following query.
update table1 s left join
(
SELECT min( tsDate ) AS MinDates, max( tsDate ) AS MaxDates, tsTask
FROM table2
group by `tsTask`
) AS t ON s.stCode=t.tsTask
set s.stActFrom= t.MinDate,
s.stActTo = t.MaxDate
WHERE s.stActTo='0000-00-00 00:00:00' and s.stStatus=100

can i use aggregation function (LAST) in mysql?

can i use aggregation function (LAST) in mysql??
if yes then why give me error for following query::
SELECT `user_id`,last(`value`)
FROM `My_TABLE`
group by `user_id`
ERROR:: 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 '(value) FROM My_TABLE group by user_id' at line 1
EDIT:: I got answer "last" is not used in MySql. then How to perform it in MySql??
No, There is nothing called LAST in mysql
See the list of aggregated function
EDIT
You can perform the same something like this
select f.user_id, f.value
from (
select MAX(value) as maxval
from my_table group by user_id
) as x inner join my_table as f on f.value = x.maxval
Something like this -
SELECT * FROM table1 t1
JOIN (SELECT depno, MAX(id) max_id FROM table1 GROUP BY depno) t2
ON t1.depno = t2.depno AND t1.id = t2.max_id
There is no "last" function defined in MySQL. Are you just trying to get the last (newest) record?
If so:
SELECT `user_id`, `value`
FROM `My_TABLE`
ORDER BY `user_id` DESC
LIMIT 1;
or
SELECT `user_id`, `value`
FROM `My_TABLE`
WHERE `user_id` = (SELECT MAX(`user_id`));
Because there is no such function called as last() in mysql..
Try to use group, order by clause in mysql
The best way I found how to handle this:
SELECT user_id, json_unquote(json_extract(json_objectagg('value', value), '$.value'))
FROM my_table
GROUP BY user_id