I have this mysql query:
select `sc_documentos`.*, `sc_clientesproveedores`.`nombre` as `cliente`,
`sc_documentos`.`descripcion` as `desc`, SUM(sc_documentos.total) AS sumaTotal,
`sc_series`.`nombre` as `serie`
from `sc_documentos`
inner join `sc_clientesproveedores` on `sc_clientesproveedores`.`id` = `sc_documentos`.`idCliente`
inner join `sc_series` on `sc_series`.`id` = `sc_documentos`.`idSerie`
where (`sumaTotal` >= 100 and `sumaTotal` <= 200 and `tipo` like '%presupuesto%'
and `sc_documentos`.`idUsuario` = 1682)
and `sc_documentos`.`deleted_at` is null order by `sc_documentos`.`created_at` desc
Mysql says: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sumaTotal' in 'where clause'
Please help I think that I can use more than one select o some, but don't know how...
In your query sumaTotal is an alias of SUM(sd_documentos.total). So either use HAVING sumaTotal >= 100 and sumaTotal <= 200 or WHERE SUM(sc_documentos.total) >= 100 AND SUM(sc_documentos.total) <= 200
sumaTotal is an alias of SUM part in your query so you can change the query like that:
select `sc_documentos`.*, `sc_clientesproveedores`.`nombre` as `cliente`,
`sc_documentos`.`descripcion` as `desc`,
`sc_series`.`nombre` as `serie`
from `sc_documentos`
inner join `sc_clientesproveedores` on `sc_clientesproveedores`.`id` = `sc_documentos`.`idCliente`
inner join `sc_series` on `sc_series`.`id` = `sc_documentos`.`idSerie`
HAVING (SUM(sc_documentos.total) >= 100 and SUM(sc_documentos.total) <= 200 and `tipo` like '%presupuesto%'
and `sc_documentos`.`idUsuario` = 1682)
and `sc_documentos`.`deleted_at` is null order by `sc_documentos`.`created_at` desc
This runs after simialbi instructions: (thanks again)
select `sc_documentos`.*, `sc_clientesproveedores`.`nombre` as `cliente`,
`sc_documentos`.`descripcion` as `desc`, SUM(sc_documentos.total) AS sumaTotal,
`sc_series`.`nombre` as `serie` from `sc_documentos`
inner join `sc_clientesproveedores` on `sc_clientesproveedores`.`id` = `sc_documentos`.`idCliente`
inner join `sc_series` on `sc_series`.`id` = `sc_documentos`.`idSerie`
where (`tipo` like '%presupuesto%' and `sc_documentos`.`idUsuario` = 1682) and `sc_documentos`.`deleted_at` is null
group by `idCliente` having sumaTotal>=600 and sumaTotal<=800
order by `sc_documentos`.`created_at` desc
Related
When I add the nested query for invCount, my query time goes from .03 sec to 14 sec. The query works and I get correct values, but it is very, very slow in comparison. Is that just because I have to many conditions in that query? When I take it out and still have the second nested query, the time is still .03 secs. There is clearly something about the first nested query the database doesn't like, but I am not seeing what it is. I have a foreign key set for all the inner join lines too. Any help or ideas would be appreciated.
SELECT a.*,
f.name,
f.partNumber,
f.showInAdminStore,
f.showInPublicStore,
f.productImage,
r.mastCatID,
(SELECT COUNT(b.inventoryID)
FROM storeInventory b
INNER JOIN events c ON c.eventID = b.eventID
WHERE b.pluID = a.pluID
AND b.listPrice = a.listPrice
AND b.unlimitedQty = a.unlimitedQty
AND (b.packageID = a.packageID OR (b.packageID IS NULL AND a.packageID IS NULL))
AND b.orderID IS NULL
AND c.isOpen = '1'
AND b.paymentTypeID <= '2'
AND (b.inCart < '$cartTime' OR b.inCart IS NULL) ) AS invCount,
(SELECT COUNT(x.inventoryID)
FROM storeInventory x
WHERE x.packageID = a.inventoryID) AS packageCount
FROM storeInventory a
INNER JOIN storePLUs f ON f.pluID = a.pluID
INNER JOIN storeCategories r ON r.catID = f.catID
INNER JOIN events d ON d.eventID = a.eventID
WHERE a.storeFrontID = '1'
AND a.orderID IS NULL
AND a.paymentTypeID <= '2'
AND d.isOpen = '1'
GROUP BY a.packageID, a.unlimitedQty, a.listPrice, a.pluID
Table from query output
UPDATE: 12/12/2022
I changed the line checking the packageID to "AND (b.packageID <=> a.packageID)" as suggested and that cut my query time down to 7.8 seconds from 14 seconds. Thanks for the pointer. I will definitely use that in the future for NULL comparisons.
using "count(*)" took about half a second off. When I take the first nested query out, it drops down to .05 seconds even with the other nested queries in there, so I feel like there is still something causing issues. I tried running it without the other "AND (b.inCart < '$cartTime' OR b.inCart IS NULL)" line and that did take about a second off, but no where what I was hoping for. Is there an operand that includes NULL on a less than comparison? I also tried running it without the inner join in the nested query and that didn't change much at all. Of course removing any of that, throughs the values off and they become incorrect, so I can't run it that way.
Here is my current query setup that still pulls correct values.
SELECT a.*,
f.name,
f.partNumber,
f.showInAdminStore,
f.showInPublicStore,
f.productImage,
r.mastCatID,
(SELECT COUNT(*)
FROM storeInventory b
INNER JOIN events c ON c.eventID = b.eventID
WHERE b.pluID = a.pluID
AND b.listPrice = a.listPrice
AND b.unlimitedQty = a.unlimitedQty
AND (b.packageID <=> a.packageID)
AND b.orderID IS NULL
AND c.isOpen = '1'
AND b.paymentTypeID <= '2'
AND (b.inCart < '$cartTime' OR b.inCart IS NULL) ) AS invCount,
(SELECT COUNT(x.inventoryID)
FROM storeInventory x
WHERE x.packageID = a.inventoryID) AS packageCount
FROM storeInventory a
INNER JOIN storePLUs f ON f.pluID = a.pluID
INNER JOIN storeCategories r ON r.catID = f.catID
INNER JOIN events d ON d.eventID = a.eventID
WHERE a.storeFrontID = '1'
AND a.orderID IS NULL
AND a.paymentTypeID <= '2'
AND d.isOpen = '1'
GROUP BY a.packageID, a.unlimitedQty, a.listPrice, a.pluID
I am not familiar with the term 'Composite indexes' Is that something different than these?
Screenshot of ForeignKeys on Table a
I think
AND (b.packageID = a.packageID
OR (b.packageID IS NULL
AND a.packageID IS NULL)
)
can be simplified to ( https://dev.mysql.com/doc/refman/8.0/en/comparison-operators.html#operator_equal-to ):
AND ( b.packageID <=> a.packageID )
Use COUNT(*) instead of COUNT(x.inventoryID) unless you check for not-NULL.
The subquery to compute packageCount seems strange; you seem to count inventories but join on packages.
The need to reach into another table to check isOpen is part of the performance problem. If eventID is not the PRIMARY KEYforevents, then add INDEX(eventID, isOpen)`.
Some other indexes that may help:
a: INDEX(storeFrontID, orderID, paymentTypeID)
a: INDEX(packageID, unlimitedQty, listPrice, pluID)
b: INDEX(pluID, listPrice, unlimitedQty, orderID)
f: INDEX(pluID, catID)
r: INDEX(catID, mastCatID)
x: INDEX(packageID, inventoryID)
After OP's Update
There is no way to do (x<y OR x IS NULL) except by switching to a UNION. In your case, it is pretty easy to do the conversion. Replace
( SELECT COUNT(*) ... AND ( b.inCart < '$cartTime'
OR b.inCart IS NULL ) ) AS invCount,
with
( SELECT COUNT(*) ... AND b.inCart < '$cartTime' ) +
( SELECT COUNT(*) ... AND b.inCart IS NULL ) AS invCount,
Revised indexes:
storePLUs:
INDEX(pluID, catID)
storeCategories:
INDEX(catID, mastCatID)
events:
INDEX(isOpen, eventID)
storeInventory:
INDEX(pluID, listPrice, unlimitedQty, orderID, packageID)
INDEX(pluID, listPrice, unlimitedQty, orderID, inCart)
INDEX(packageID, inventoryID)
INDEX(storeFrontID, orderID, paymentTypeID)
i tried to run this query:
WITH updateables AS (
SELECT id_product, quantity
FROM products_order
WHERE id_order = 1239 AND state = 10
)
UPDATE product
SET product.stock = updateables.quantity
WHERE product.id_product = updateables.id_product```
But this trows:
Error Code: 1054. Unknown column 'updateables.id_product' in 'where clause```
Defining a CTE using the WITH clause does not implicitly join that CTE to other tables in your query.
You would need to use a JOIN in your query to the CTE:
WITH updateables AS (
SELECT id_product, quantity
FROM products_order
WHERE id_order = 1239 AND state = 10
)
UPDATE product JOIN updateables ON product.id_product = updateables.id_product
SET product.stock = updateables.quantity;
I'm having trouble adding a condition on aliases is_paid, is_overdue and is_outstanding in the following query:
SELECT r.doc_number,
r.doc_date,
r.due_date,
r.currency,
r.amount,
r.vat,
r.vatammount,
(r.amount + r.vatammount) final_amount,
r.currency,
b.boq_id,
b.boq_comp_id,
b.boq_client_id,
b.boq_agency,
b.boq_date,
b.boq_orders,
b.receivable_id,
c.comp_name,
crm.`cn-name-first`,
crm.`cn-name-last`,
bi.inv_path,
(SELECT SUM(amount_recieved)
FROM receivables_payments
WHERE r_id = b.receivable_id) total_amount_received,
IF (r.amount + r.vatammount =
(SELECT SUM(amount_recieved)
FROM receivables_payments
WHERE r_id = b.receivable_id),
'1',
'0') AS is_paid,
IF (CURRENT_DATE >= r.due_date
AND r.amount + r.vatammount !=
(SELECT SUM(amount_recieved)
FROM receivables_payments
WHERE r_id = b.receivable_id),
'1',
'0') AS is_overdue,
IF (r.due_date < CURRENT_DATE
AND r.amount + r.vatammount !=
(SELECT SUM(amount_recieved)
FROM receivables_payments
WHERE r_id = b.receivable_id),
'1',
'0') AS is_outstanding
FROM receivables r
LEFT JOIN boq b ON b.receivable_id = r.id
LEFT JOIN boq_invoices bi ON bi.inv_boq_id = b.boq_id
LEFT JOIN comp_companies c ON c.comp_id = b.boq_comp_id
LEFT JOIN crm_contacts crm ON crm.contact_id = b.boq_client_id
WHERE r.status = 'active'
AND r.doc_type = 'inv'
AND b.boq_status = 'active'
AND is_paid = '1'
ORDER BY r.doc_date DESC LIMIT 10
Is there any way to modify this query and to make it possible to add a condition on those three aliases?
use alias in where condition .. is not allowed . because .is not possibile
the query code is evaluted based on a specified order .. starting from FROM then
WHERE clause and last the SELECT and the column alias so .. when the where is performed the column alias is not available at the query
You could try with an having condition because having work on the result of the query and not on the raw rows value .. (this could have effect on performance ..because all the query is performed and only the result is filtered by having)
I am having trouble joining mutiple tables.
I can get 2 to join, but I get an error when I try to join all three:
This works:
SELECT `capman`.`cman_code`, `capman`.`cman_name`, `caprange`.`cran_name`, `caprange`.`cran_mantextcode`
from capman, caprange
LEFT JOIN `capder` ON `caprange`.`cran_code` = `capder`.`cder_rancode`
AND `capder`.`cder_discontinued` = '0000-00-00 00:00:00'
LIMIT 10
And this works:
SELECT `capman`.`cman_code`, `capman`.`cman_name`, `caprange`.`cran_name`, `caprange`.`cran_mantextcode`
from capman
LEFT JOIN `caprange` ON `caprange`.`cran_mantextcode` = `capman`.`cman_code` WHERE LCASE(cman_name) IN ('arbarth','alfa romeo','aston')
LIMIT 10
But when I try to put them together like this:
SELECT `capman`.`cman_code`, `capman`.`cman_name`, `caprange`.`cran_name`, `caprange`.`cran_mantextcode`
from capman, caprange
LEFT JOIN `caprange` ON `caprange`.`cran_mantextcode` = `capman`.`cman_code` WHERE LCASE(cman_name) IN ('arbarth','alfa romeo','aston')
LEFT JOIN `capder` ON `caprange`.`cran_code` = `capder`.`cder_rancode` AND `capder`.`cder_discontinued` = '0000-00-00 00:00:00'
LIMIT 10
I get the error:
Not unique table/alias: 'caprange'
Not sure if you mean to join caprange twice. If so, you can use alias to point to the right table. The query could be rewritten as:
SELECT `capman`.`cman_code`, `capman`.`cman_name`, `caprange`.`cran_name`, `caprange`.`cran_mantextcode`
from capman, caprange r1
LEFT JOIN `caprange` r2 ON r2.`cran_mantextcode` = `capman`.`cman_code` WHERE LCASE(cman_name) IN ('arbarth','alfa romeo','aston')
LEFT JOIN `capder` ON r1.`cran_code` = `capder`.`cder_rancode` AND `capder`.`cder_discontinued` = '0000-00-00 00:00:00'
LIMIT 10
Since I am not sure which table you are joining with caprange, replace r1/r2 in the query with the right alias.
I'm trying to make the following query work but it seems to contain an error that I can't find. The query in question is:
select
A.*,
SD.*
from
(
( select
V71.Sollevamento_idAttrezzatura,
V71.Num_id_ON,
V71.Affidato_INAIL,
V71.Esito_Verifica,
V71.Prima_Verifica,
V71.Sospensione,
V71.Data_Verbale,
V71.Scadenza,
CO.Nome,
CO.Cognome,
CO.CF,
V71.Costo,
V71.Sconto as ScontoVerbale,
V71.Maggiorazione as MaggiorazioneVerbale,
V71.Indirizzo,
V71.Comune,
V71.Cap,
V71.Provincia,
V71.Regione
from
Verbale_Art71 as V71
join Collaboratore as CO
on V71.Codice_Tecnico = CO.Codice_Collaboratore
where
V71.Data_Verbale >= '2014-01-01'
and V71.Data_Verbale <= '2014-12-31' ) as VC
join
( select
S.Natura,
S.Anno_Immatricolazione,
S.Codice_Attribuzione_Matricola,
S.Provincia_Immatricolazione,
S.Numero_Matricola,
S.idSpecifica,
S.N_Panieri_Idroestrattori,
S.Modello,
S.Numero_Fabbrica,
S.Anno_Costruzione,
S.Sconto as ScontoAttr,
S.Maggiorazione as MaggiorazioneAttr,
P71.Tipo_Attr,
S.Sede,
S.idAttrezzatura
from
Sollevamento as S
join Periodicita_Art71 as P71
on S.idPeriodicita = P71.idPeriodicita ) as SP
on VC.Sollevamento_idAttrezzatura = SP.idAttrezzatura
) as A
join
( select
D.Ragione_Sociale,
D.Indirizzo,
D.Cap,
D.Comune,
D.Provincia,
D.Regione,
D.CF as CF_Ditta,
D.P_IVA,
SC.idSede
from
Ditta as D
join Sede as SC
on SC.Ditta = D.idDitta ) as SD
on ATTR71.Sede = SD.idSede
MySQL notifies that the syntax error is near as A join (select D.Ragione_Sociale, D.Indirizzo, D.Cap, D.Comune, D.Provincia
Anyone can help me find it? I've been looking for it since early morning and I'm going nuts. Thank anyone that will help me in advance, Giacomo
You are missing a select clause
select A.*, SD.*
from
(/* You are missing a select * from here */
(
select ..
) as VC
join
Try with
select A.*, SD.*
from
(select * from
(
select V71.Sollevamento_idAttrezzatura, V71.Num_id_ON, V71.Affidato_INAIL, V71.Esito_Verifica, V71.Prima_Verifica, V71.Sospensione, V71.Data_Verbale, V71.Scadenza, CO.Nome, CO.Cognome, CO.CF, V71.Costo, V71.Sconto as ScontoVerbale, V71.Maggiorazione as MaggiorazioneVerbale, V71.Indirizzo, V71.Comune, V71.Cap, V71.Provincia, V71.Regione
from Verbale_Art71 as V71
join
Collaboratore as CO
on V71.Codice_Tecnico = CO.Codice_Collaboratore
where V71.Data_Verbale >= '2014-01-01' and V71.Data_Verbale <= '2014-12-31'
) as VC
join
(
select S.Natura, S.Anno_Immatricolazione, S.Codice_Attribuzione_Matricola, S.Provincia_Immatricolazione, S.Numero_Matricola, S.idSpecifica, S.N_Panieri_Idroestrattori, S.Modello, S.Numero_Fabbrica, S.Anno_Costruzione, S.Sconto as ScontoAttr, S.Maggiorazione as MaggiorazioneAttr, P71.Tipo_Attr, S.Sede, S.idAttrezzatura
from Sollevamento as S
join
Periodicita_Art71 as P71
on S.idPeriodicita = P71.idPeriodicita
) as SP
on VC.Sollevamento_idAttrezzatura = SP.idAttrezzatura
) as A
join
(
select D.Ragione_Sociale, D.Indirizzo, D.Cap, D.Comune, D.Provincia, D.Regione, D.CF as CF_Ditta, D.P_IVA, SC.idSede
from Ditta as D
join Sede as SC
on SC.Ditta = D.idDitta
) as SD
on ATTR71.Sede = SD.idSede
Your final alias
on ATTR71.Sede = SD.idSede
should be
on A.Sede = SD.idSede
You can see it easily once the formatting / readability is better. The "A" was the alias result of the first JOINs using the ATTR71 alias table... The last join was only to the "A" alias and not the ATTR71 table.