MySql subquery inside View - mysql

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.

Related

mysql vs mariadb joins gives different results

as part of migration from mysql to mariadb, i face a query that gives different results between mysql and mariadb when joining the same table twice to perfrom cross search. for some reason that i can't understand, on mysql the query works perfect but on the mariadb side, the query gives no results
this is my query:
SELECT a.*, c.title as category, c.seperator
FROM MainLink
LEFT JOIN MainLink AS ArticleLink on ArticleLink.tag_id = MainLink.tag_id
LEFT JOIN articles AS a on a.id = ArticleLink.object_id
INNER JOIN categories c on a.category_id=c.id
WHERE ArticleLink.module_id = '20' AND MainLink.module_id = '12' AND MainLink.object_id = '1625'
GROUP BY a.id ORDER BY a.id DESC
MainLink Table:
|tag_id|int(10)|UNSIGNED
|module_id|int(10)|UNSIGNED
|object_id|int(10)UNSIGNED
articles table:
|id|int(10)|UNSIGNED
|title|varchar(1000)|UNSIGNED
|category_id|int(10)|UNSIGNED
categories table:
|id|int(10)|UNSIGNED
|title|varchar(100)|UNSIGNED

Mysql - I have 3 unique tables, need a hint on getting the details with the count

I have 3 tables which are interconnected and i want to select columns from two tables and counts from table 3. If anyone is aware on this, any hint would be appreciated.
Below is the sql i tried, but the count is getting repeated
SELECT distinct p.p_id, p.p_f6, p.p_l4,m.m_id, (
SELECT COUNT(*)
FROM ttokens t where t.pdetail_id = p.pdetail_id
) AS token_count
FROM tparking p,ttokens t LEFT join ttokens_md m ON t.trefn_id = m.trefn_id
WHERE t.pdetail_id = p.pdetail_id
You can try to use JOIN with subquery to get your count instead of selcet subquery.
SELECT p.p_id, p.p_f6, p.p_l4,m.m_id,t.cnt
FROM tparking p
JOIN (
SELECT pdetail_id,COUNT(*) cnt
FROM ttokens
GROUP BY pdetail_id
) t ON t.pdetail_id = p.pdetail_id
LEFT join ttokens_md m ON t.trefn_id = m.trefn_id
Note
I would use JOIN instead of , comma with where condition to connect two tables,, is an old style.

MySQL Workbench, how to fix "SELECT is not valid at this position for this server version" error

As the title says, the MySQL Workbench is reporting an error. The error is from the software, not from MySQL, as far as I can tell. However, it's preventing me from bug fixing the code, as it will not work on my website.
Originally I used this query, which works perfectly fine, but is very slow, as it has a select inside the inner join.
SELECT *
FROM modeller AS m
INNER JOIN
bilder AS b ON b.ID = (
SELECT ID FROM bilder AS b2
WHERE b2.modellID = m.id
ORDER BY filnavn
DESC LIMIT 1
)
order by m.fornavn
So I've been looking around for a replacement and ended up with this:
SELECT *
FROM (
SELECT p.*,
m.*,
row_number() over (partition by m.id order by filnavn desc) rono
FROM modeller AS m
INNER JOIN bilder AS B
on B.modellID= m.ID
) x
WHERE x.rono = 1
What this query is supposed to do it to look through a database of 3D models ('modeller') and get the latest picture added to that model in the table 'bilder'. This is a one-to-many connection, bilder has modellID as a column, which corresponds to the ID in modeller.
Instead, I get the error
SELECT is not valid at this position for this server version, expecting : '(', WIDTH
Any tips hints or suggestions are greatly appreciated, especially something that not only circumvents the Workbench-error, but also improves on my initial query.
An alternative you could try is:
SELECT *
FROM modeller AS m
INNER JOIN (
SELECT model1ID, MAX(filnavn) AS filnavnMax
FROM bilder
GROUP BY model1ID
) AS maxVals
ON m.id = maxVals.model1ID
INNER JOIN bilder AS b
ON maxVals.model1ID = b.model1ID AND maxVals.filnavnMax = b.filnavn
ORDER BY m.fornavn
;
However, if filnavn is not unique for a model1ID, you could end up with more results than your original query returned. But if that is the case, your original query could provide inconsistent results (since there is no guarantee the ID returned from the original subquery would be consistent when there is more than one row with the highest filnavn)

MySQL replace minus with left join

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;

Multiple Joins Mysql Doubles SUM values

Im trying to make a a query, but its doubling the Sum values
SELECT
cidades.id AS id,
cidades.name AS municipio,
Sum(conjuntos.n_uhs) AS uh,
programas.id AS programa,
conjuntos.name
FROM
conjuntos
Inner Join conjuntos_programas ON conjuntos_programas.conjunto_id = conjuntos.id
Inner Join programas ON programas.id = conjuntos_programas.programa_id
Inner Join cidades ON conjuntos.cidade_id = cidades.id
WHERE
conjuntos.situation_id = 2
GROUP BY
conjuntos.cidade_id
ORDER BY
municipio ASC
You've got duplicate rows, you can check this by removing the group by and the SUM(... from your query.
Change the query as follows and tell me if that fixes to problem.
SELECT DISTINCT
cidades.id AS id,
cidades.name AS municipio,
SUM(conjuntos.n_uhs) AS uh,
programas.id AS programa,
conjuntos.name
FROM conjuntos
INNER JOIN conjuntos_programas ON conjuntos_programas.conjunto_id = conjuntos.id
INNER JOIN programas ON programas.id = conjuntos_programas.programa_id
INNER JOIN cidades ON conjuntos.cidade_id = cidades.id
WHERE conjuntos.situation_id = 2
GROUP BY conjuntos.cidade_id
ORDER BY municipio ASC
It sounds like you have a one to many relationship between two or more of your tables to be doing this. Try doing a SELECT * and start debugging your query to see where it is duplicating the rows.