How to fix a SQL Join query on MySQL Workbench - mysql

I'm working on MySQL Workbench and A SQL join query is not working for me.
SELECT OI.OrderName, count(OI.OrderNo)
FROM onns.es_orderInfo as OI
JOIN onns.es_order as O
ON OI.OrderNo = O.OrderNo
JOIN onns.es_orderGoods OG
ON O.OrderNo = OG.OrderNo
GROUP BY OI.OrderName;
I don't get any response; the code keeps running.
Could anyone help me figure out my mistake?

Related

Doctrine subquery in symfony

I've got a little issue. I've got to do a query that is based on a subquery.
The SQL query I need to get to is:
FROM testing t
WHERE t.id NOT IN (
SELECT h.task_id
FROM task t
INNER JOIN hardware h ON h.task_id = t.id
);
What I have for a moment is the basic query and I don't really understand how to do that subquery..
$test->createQueryBuilder('t')
->select('t.id')
->getQuery()->getArrayResult();

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.

Joining two working sql queries gets wrong results - Where am i going wrong

I have two queries that individually return the correct data, but when I tried to join them the returned data is wrong.
Query one is:
SELECT op.orders_id, op.products_id, op.products_name, op.final_price, op.products_quantity
FROM orders_products op
WHERE op.orders_id = 21535
Query two is:
SELECT opa.products_options_values_id, opa.products_prid
FROM orders_products_attributes opa
WHERE opa.orders_id = 21535
The combined query I tried was:
SELECT op.orders_id, op.products_id, op.products_model, op. products_name, op.final_price, op.products_quantity, opa.products_options_values_id, opa.products_prid
FROM orders_products op
JOIN orders_products_attributes opa
ON op.orders_id = opa.orders_id
WHERE op.orders_id = 21535
Fiddle is here: http://www.sqlfiddle.com/#!8/ef9be/5
I've no idea how to fix this. I tried Group BY on opa.products_options_values_id but that caused issues because two results of 40 are in the data result.
Any help appreciated.
In your join condition, you used orders_id. You should use orders_products_id like this:
SELECT op.orders_id, op.products_id, op.products_model, op. products_name,
op.final_price, op.products_quantity, opa.products_options_values_id,
opa.products_prid
FROM orders_products op
JOIN orders_products_attributes opa ON op.orders_products_id = opa.orders_products_id
WHERE op.orders_id = 21535

mysql 5.0 upgrade to 5.1 Match function

I migrated a website's database from MySQL 5.0.95 to MySQL 5.1.59
When i try to do a search i get different results
for example:
When i search for "MTH" on the old database i get one result,
but when i search on the new database for "MTH" i do not get any results
However when i do a search for "accountents" i DO get the SAME results with both databases!?!
so only some words shows different results, not all
I narrowed it down to the "match" function from mysql.
Anyone who has had the same problem wen migrating a website perhaps?
This is my sql query:
SELECT
a.addressid,
a.status,
a.name,
a.subname,
a.subtitle,
a.city,
a.googlemaps,
a.address,
a.zipcode,
a.pobox,
a.poboxzipcode,
a.phone,
a.fax,
a.website,
a.intro,
a.packageid_fk,
a.enddate,
DATE_FORMAT(a.enddatenew, '%d-%m-%Y') AS endingdate,
p.name package,
m.themeid_fk
FROM
addresses AS a
INNER JOIN addresses_subcategories AS a_s
ON a_s.addressid_fk = a.addressid
LEFT JOIN packages AS p
ON p.packageid = a.packageid_fk
INNER JOIN subcategories AS s
ON s.subcategoryid = a_s.subcategoryid_fk
INNER JOIN maincategories AS m
ON m.maincategoryid = s.maincategoryid_fk
WHERE
a.status = 1
AND
MATCH(a.name, a.subname, a.sponsorkeywords, a.extrakeywords, a.website)
AGAINST('MTH*' IN BOOLEAN MODE)
AND
a.websiteid_fk = 1
GROUP BY a.addressid
ORDER BY a.packageid_fk DESC, a.sort ASC, a.name ASC
PS. This very same query works great on MySQL 5.0, however on 5.1 it does not show the same results
Seems it had to do with the ft_min_word_len variable. after contacting my webhost they changed it and now it words again

Indexing on MySQL

I have a bulk query with subquery. My query works fine when I run it on development server, but when I've try it pn the live server, the query takes too much time to produce an output. I think it's because of a big data on the live server. Can anyone help me on how to index query on MySQL so that it will lessen the time execution.
Here is my query:
SELECT prd.fldemployeeno AS Empno,
(SELECT fldemployeename FROM tblprofile prf WHERE prf.fldemployeeno = prd.fldemployeeno LIMIT 0,1) AS Empname,
'01' AS `Week`,
COUNT(DISTINCT isAud.fldid) AuditedFiles,
COUNT(qua.seqid) ErrorCount,
COUNT(DISTINCT qua.fldid) OrdersWithError
FROM tbldownloadITL dwn
INNER JOIN tblproductionITL prd
ON dwn.fldid = prd.fldglobalid
INNER JOIN (SELECT p.fldemployeeno,fldglobalid,p.fldstarttime,COALESCE(q.fldstarttime,p.fldstarttime) `AuditDate`
FROM tblproductionitl p
LEFT JOIN tblqualityaudit q
ON p.fldemployeeno=q.fldemployeeno
AND p.fldstarttime=q.fldprodstarttime
AND p.fldglobalid=q.fldid
WHERE p.fldprojectgroup='PROJGROUP') temp
ON prd.fldglobalid=temp.fldglobalid
AND prd.fldemployeeno=temp.fldemployeeno
AND prd.fldstarttime=temp.fldstarttime
INNER JOIN tblisauditedITL isAud
USING (fldid)
LEFT JOIN tblqualityaudit qua
ON qua.fldid = dwn.fldid
AND qua.fldbusunit = dwn.fldbusunit
AND qua.fldprojectGroup = dwn.fldprojectGroup
AND qua.fldemployeeno = prd.fldemployeeno
AND qua.fldprodstarttime = prd.fldstarttime
AND qua.flderrorstatus != 'NOT ERROR'
LEFT JOIN tblerrorcategory
USING (flderrorcategoryid)
LEFT JOIN tblerrortypes
USING (flderrortypeid)
WHERE dwn.fldbusunit = 'BUSUNIT'
AND dwn.fldprojectGroup = 'PROJGROUP'
AND temp.AuditDate BETWEEN '2011-07-29 00:00:00' AND '2011-07-29 23:59:59'
GROUP BY prd.fldemployeeno
ORDER BY Empname
Here is also the description of the query:
I would suggest installing Sphinx on the your server if you have the access. That way you can have an indexed resource at your finger tips for extremely fast searching, on top of that you can add the execution of what is called a 'delta' index to allow for real time updating of your mysql database. It is highly customizable. Hopefully this will help you out.
http://sphinxsearch.com/