MySQL replace minus with left join - mysql

I am working on migrating from Oracle to MySQL and currently stuck on converting query with MINUS operation. I know, I have to use left join, but somehow my query is not giving the exact rows as it is in Oracle. The query is very simple with with select on views "V_*".
On Oracle:
select s.section, c.title from course c, v_staff_active s
minus
select section,title from v_rep_attended_by_section
On Mysql, I converted it like below:
select * from
( select s.section, c.title from course c, v_staff_active s) x
left join
( select section,title from v_rep_attended_by_section) y on x.section = y.section
where y.section is null;
But not getting the exact result or number of records.
Can you please help me out as I am very much new to MySQL.
Thanks in advance.

Your query should look like:
select s.section, c.title
from course c
left join v_staff_active s on c.section = s.section
and c.title = s.title
where s.section is null and s.title is null;

Related

MySql subquery inside View

I created a view using MySql subquery, it worked on my localhost,
it failed in production because Mysql version is 5.6, it doesn't support subqueries on views, so i need help rewriting the whole query,
I tried with UNION ALL but it tells me i need the same amount of columns for each Select.
New query:
CREATE VIEW vista_inventarionacional AS
SELECT T.Ultima_Actualizacion,
r.nombre AS Red,
co.nombre AS Comuna,
equipo.nombre AS Descripcion,
equipo.marca AS Marca,
equipo.modelo AS Modelo,
p.nombre AS Parametro,
equipo.nserie AS NumSerie,
equipo.fecha_compra AS Fecha_Recepcion,
equipo.id_mma AS MMA,
equipo.fecha_inicio_uso AS FechaInstalacion,
equipo.fecha_vigencia as FechaBaja,
e.nombre AS LugarInstalacion,
eq_etd.nombre AS EstadoEquipo
FROM equipo
LEFT JOIN equipo_estacion AS eq_est ON eq_est.equipo_id = equipo.id
JOIN estacion AS e ON e.id = eq_est.estacion_id
LEFT JOIN red AS r ON r.id = e.red_id
LEFT JOIN comuna AS co ON co.id = e.comuna_id
LEFT JOIN equipo_parametro AS eq_p ON eq_p.equipo_id = equipo.id
JOIN parametro AS p ON p.id = eq_p.parametro_id
LEFT JOIN equipo_estado AS eq_etd ON eq_etd.id = equipo.equipo_estado_id
WHERE eq_est.estado = 'activo';
UNION ALL
SELECT equipo_id, MAX(fecha) AS Ultima_Actualizacion
FROM transferencia GROUP BY equipo_id
Old Query with Subquery:
Thanks in advance!
Your query:
UNION ALL
SELECT equipo_id, MAX(fecha) AS Ultima_Actualizacion
FROM transferencia GROUP BY equipo_id
is incorrect. Because in 1st query you have 14 column you try to union all with 2 column.
You can use :
SELECT equipo_id, MAX(fecha), null,null,null,null,null,null,null,null,null,null,null,null, AS Ultima_Actualizacion
FROM transferencia GROUP BY equipo_id;
Also View's select contains a subquery error: You can solve this. Use mysql workbrench to connect mysql DB. and run create view script from mysqlworkbrench.
It will work.

How to convert code written in INNER JOIN to Subquery

Need some help with converting code from Join statement into Subquery.
I need to remove GROUP BY from it somehow, when converted into Subquery and don't know how.
Managed to put small portion of subquery at the end of the code, don't know how to do rest.
Need some help, thank you.
Here is the sample of the code: (need to convert into SQL Server syntax)
SELECT
b.Number, t.IDTyre, SUM(c.Price)
FROM Tyre AS t
INNER JOIN Bill AS b ON t.BillID = b.IDBill
INNER JOIN Customer AS c ON c.TyreID = t.IDTyre
GROUP BY b.Number, t.IDTyre
HAVING SUM(c.Price) < 3000 OR t.IDTyre NOT IN (SELECT c.TyreID FROM Customer AS c)
Check if the below query works:
SELECT
(Select b.Number From Bill AS b Where b.IDBill = t.BillID) as Number,
t.IDTyre as TyreID,
(Select SUM(c.Price) From Customer AS c Having SUM(c.Price) < 3000 OR t.IDTyre NOT IN (SELECT Distinct c.TyreID FROM Customer AS c) And c.TyreID = t.IDTyre) as Price
FROM Tyre AS t
Why are you trying to convert this to Sub Query?
JOINS are the best options while dealing with linking tables.
Also the "NOT IN" that you are trying to do at the end is also not good, you should use "NOT EXISTS". Change this to: OR NOT EXISTS (SELECT * FROM Customer AS c WHERE t.IDTyre=c.TyreID)

How to give alias to results returned after inner join in mySQL

I need to do a correlated SQL Query and for that purpose i need to provide an alias to outer query which in which I perform an inner join. I am not able to do the alias
SELECT DISTINCT(name)
FROM PERSON
INNER JOIN M_DIRECTOR AS dira
ON (dira.PID = M_DIRECTOR.PID) as dira
WHERE 9 > (
SELECT COUNT(MID) FROM M_DIRECTOR WHERE name = dira.name
) ;
I didn't really understand what you want to do, but I guess
select
distinct p.name,
count(d.MID) cnt
from
hindi2_PERSON p
inner join
hindi2_M_DIRECTOR d
on
p.PID = d.PID
group by
p.name
having count(d.MID) > 9
;
would do what you want
I dont know what you are asking and what you mean by make an alias to an eniter result ?
but you are doing
select distinct(name) as othername
which is you are selecting name and you are giving here othername as an alias
then you retrieve it in result
$row['othername']
There's still something missing. From what you write, there is a field name in the M_DIRECTOR table?
Please show all the tables and attributes involved, use an SQL Fiddle to prepare an example.
SELECT DISTINCT(name)
FROM PERSON as p
INNER JOIN (
SELECT COUNT(MID), PID FROM M_DIRECTOR WHERE name = dira.name
) as d
ON (p.PID = d.PID) ;

mysql query joining three queries into sigle query

Please help on this query, here is sql fiddler : http://sqlfiddle.com/#!2/8acc1/1
SELECT DISTINCT c.id, v.sel_category,c.curr_tittle , c.curr_desc, v.videos_desc
FROM wp_curriculum c, wp_career_vidoes v
WHERE c.id IN
(SELECT DISTINCT curr_id
FROM wp_curriculum_category
WHERE curr_category IN (2,3)) AS I
AND I.curr_category = v.sel_category
GROUP BY I.curr_category
do you mean this?
SELECT DISTINCT c.id,
v.sel_category,
c.curr_tittle ,
c.curr_desc,
v.videos_desc
FROM wp_curriculum c
INNER JOIN wp_curriculum_category cat
ON c.id = cat.curr_id
INNER JOIN wp_career_vidoes v
ON v.sel_category = cat.curr_category
WHERE cat.curr_category IN (2,3)
SQLFiddle Demo
To further gain more knowledge about joins, visit the link below:
Visual Representation of SQL Joins

Getting data differences between two queries

I have two queries that result two result sets i need to compare both the result sets and need to display the differences between them.Hope i will get good support.Thank you.These are my queries
Query:1
SELECT distinct c.sid_ident,c.fix_ident from corept.std_sid_leg as c INNER JOIN (SELECT sid_ident, transition_ident, max(sequence_num) seq, route_type FROM corept.std_sid_leg WHERE data_supplier='J' AND airport_ident='KBOS' GROUP BY sid_ident,transition_ident) b ON c.sequence_num=b.seq and c.sid_ident = b.sid_ident and c.transition_ident =b.transition_ident WHERE c.data_supplier='J' and c.airport_ident='KBOS';
Query:2
SELECT name,trans FROM skyplan_deploy.deploy_sids ON d.name=c.sid_ident WHERE apt = 'KBOS' AND name != trans;
Comparison is to be done on fields sid_ident in corept.std_sid_leg and name in skplan_deplay.deploy_sids. As Mysql does not support full outer join,I thought of using left join and right join and combine both the results.But i stuck up with this.Please help.I am getting syntax error while using left and right join.Thank you.
The following query should simulate a FULL OUTER JOIN in MySQL.
SELECT *
FROM A
LEFT OUTER JOIN B
ON A.NAME = B.NAME
WHERE B.ID IS NULL
UNION ALL
SELECT *
FROM B
LEFT OUTER JOIN A
ON B.NAME = A.NAME
WHERE A.ID IS NULL;
Compare the results of the with an actual FULL OUTER JOIN in SQL Server and you'll see it works.