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
Related
I am fairly new to MySQL, I wrote this query
SELECT
r.id as rID,
r.title as rTitle,
r.researcherName as rName,
r.volumeID as rVolID,
r.views as rViews,
r.downloads as rdowns,
vol.date as volDate,
vol.title as volTitle,
vol.versionID as verID,
ver.title as verTitle
FROM journalsResearches AS r
INNER JOIN versions as ver
INNER JOIN volumes as vol
ON r.volumeID = vol.id
AND r.volumeID = 12
ORDER BY r.views DESC
and expected result was 1 row and for some reason, this row has been duplicated as a result and still one row in table
You forgot a condition on the join with the 'versions' table, so your query might actually be returning one row per row in 'versions'.
Fixed with rearranging tables in JOIN statement and adding ON condition on versions
SELECT
r.id as rID,
r.title as rTitle,
r.researcherName as rResearcherName,
r.views as rViews,
r.downloads as rDownloads,
vol.id as volID,
vol.title as volTitle,
vol.date as volDate,
ver.id as verID,
ver.title as verTitle
FROM
journalsResearches AS r
INNER JOIN volumes as vol
INNER JOIN versions as ver
ON r.volumeID = vol.id
AND vol.versionID = ver.id
AND vol.id = 12
ORDER BY r.views DESC
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.
I have a problem with my mysql output on the workbench. I'm trying to get the company(qualified_name) together with an timestamp(updated).
Company and the timestamp are in different tables(the headers are further down). Ignore the id's there were only for me to compare
SELECT entity_id, c.id, o.id, o.updated,
res_companies.qualified_name
FROM str_entities as o
JOIN str_entities c
ON c.id = o.owner_id
AND c.client = "client"
LEFT JOIN res_companies
ON entity_id = c.id
where o.status = "active"
AND o.entity_type_id = 7
MySql Workbench gives me that output
Here is the table header for res_companies
And here is the heder from str_entities
try this and tell us what you get
Select count(*) from res_companies rc
Where exists (Select * from str_entities se
Where id = rc.entity_id
and client = "client"
and exists (Select * from str_entities
Where owner_id = se.id))
Are there any records there ?
If the result is zero (0), then the last column (from the left join) is null because there are simply no records in the table that match the criteria you have specified.
when i write a MySQL query, there occur a problem. here is my query
SELECT
SUM(view_product_count_details.view_product_count) AS count_sum,
product_details.product_name,
product_details.product_url,
product_details.product_price,
product_image_details.product_image_name,
main_category_details.main_category_url,
sub_category_details.sub_category_url
FROM
view_product_count_details
JOIN
product_details ON view_product_count_details.product_id_fk = product_details.product_id
JOIN
product_image_details ON product_image_details.product_id_fk = view_product_count_details.product_id_fk
JOIN
main_category_details ON product_details.product_main_cat_id = main_category_details.main_category_id
JOIN
sub_category_details ON product_details.product_sub_cat_id_fk = sub_category_details.sub_category_id
WHERE
view_product_count_details.view_product_status = 'active'
GROUP BY view_product_count_details.product_id_fk
ORDER BY count_sum DESC
LIMIT 4
Here I have multiple images for one product.the images are in table "product_image_details". this query returns count as the number of images, where I need the count of product viewed by people which is stored in table "view_product_count_details". when I just pick the count, i got the count as it is. but when i join the table "product_image details", result become wrong. Is there any way to do it in single query?
Please help me... Thanks in advance.... :)
You can do it by having an inline query. I am not sure how this will perform when you have more data.
SELECT table1.*,product_image_details.product_image_name FROM
(
SELECT
SUM(view_product_count_details.view_product_count) AS count_sum,
product_details.product_id,
product_details.product_name,
product_details.product_url,
product_details.product_price,
main_category_details.main_category_url,
sub_category_details.sub_category_url
FROM
view_product_count_details
JOIN
product_details ON view_product_count_details.product_id_fk = product_details.product_id
JOIN
product_image_details ON product_image_details.product_id_fk = view_product_count_details.product_id_fk
JOIN
main_category_details ON product_details.product_main_cat_id = main_category_details.main_category_id
JOIN
sub_category_details ON product_details.product_sub_cat_id_fk = sub_category_details.sub_category_id
WHERE
view_product_count_details.view_product_status = 'active'
GROUP BY view_product_count_details.product_id_fk
ORDER BY count_sum DESC
LIMIT 4
) table1
JOIN
product_image_details ON product_image_details.product_id_fk = table1.product_id
LIMIT 4
I want to get all fields from one table and use DISTINCT with the second table.
I have this:
SELECT stats.*,
DISTINCT(visit_log.blog_id) AS bid
FROM stats
INNER JOIN visit_log ON stats.blog_id = visit_log.blog_id
But I get this 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 'DISTINCT(visit_log.blog_id) AS bid FROM stats INNER JOIN visit_log ON stats.blog' at line 1
Any idea?
Instead of joining against visit_log, you can construct a derived table containing only the distinct blog_id values.
select stats.*, v.blog_id
from stats
inner join ( select distinct blog_id from visit_log where stats.blog_id = visit_log.blog_id ) as v
SELECT stats.*, dr.blog_id
FROM stats
INNER JOIN (SELECT DISTINCT(visit_log.blog_id) AS bid FROM visit_log) AS dr
ON stats.blog_id = dr.blog_id
You are only selecting blog_id from visit_log which is the column you are joining on. So your query is much like:
select *
from stats s
where
exists (select null from visit_log v where s.blog_id = v.blog_id)
select * from visit_log v where v.blog_id in/= (select s.blog_id from stats s)