Would like the rows with max(date) - mysql

This works in MYSQL:
SELECT hi_Historie_id,
hi_Klus_id,
hi_Datum_gedaan,
hi_Prijs,
hi_Voldaan,
hi_Datum_voldaan,
hi_Voldaan_via,
max(hi_next_date),
hi_Opmerking
FROM Historie
GROUP BY hi_Klus_id
This gives the right Result: all the rows with the hi_Klus_id with the latest date.
But than I will make a join with an other table:
LEFT OUTER JOIN Glazenwassen ON Historie.hi_Klus_id = Glazenwassen.gw_Klus_id
WHERE Historie.hi_next_date <= CURDATE()
This gives the error #1064.
Can anybody explain me why?

My suggestion would be to create a subquery to return the MAX(hi_next_date) and then join that result to both the Historie and Glazenwassen tables:
select h.hi_Historie_id,
h.hi_Klus_id,
h.hi_Datum_gedaan,
h.hi_Prijs,
h.hi_Voldaan,
h.hi_Datum_voldaan,
h.hi_Voldaan_via,
h.hi_next_date,
h.hi_Opmerking
from Historie h
inner join
(
select max(hi_next_date) hi_next_date, hi_Klus_id
from Historie
group by hi_Klus_id
) h2
on h.hi_Klus_id = h2.hi_Klus_id
and h.hi_next_date = h2.hi_next_date
left join Glazenwassen g
on h.hi_Klus_id = g.gw_Klus_id
where h.hi_next_date <= CURDATE()
When you join the subquery to the Historie table, you will want to join on both the hi_next_date and the hi_Klus_id.

Related

Unable to get complete data using mysql 3 joins query

i have 1500 records in domains table but by using this query only get 1215 records. How to modify this query to give desired outcome and better performance
SELECT
d.id, d.domain_name, d.action, d.comment, d.agent_email,
d.assigned_date, d.added_date, dd.registered_on, dd.expiry_date,
dd.updated_date, dd.acquire_price, dd.acquire_date, dd.email,
dd.effective_price, dd.registrar, dd.status, dd.servers,
count(l.lead_domain), d.domainer_email, d.current_status,
d.undeveloped, d.sedo, d.afternic, d.flippa, d.uniregistry,
d.go_daddy, d.domr, d.minimum_offer, d.buy_it_now_price,
d.way_to_find_leads, dd.tlds_taken
FROM domains d
right join domains_data dd on d.domain_name=dd.domain_name
left outer join lead_domains l on d.domain_name=l.domain_name
and d.domainer_email=l.domainer_email
group by d.domain_name
having d.action='all' and d.domainer_email='abc#gmail.com'
order by d.added_date desc;
Your results depend on the domains_data table.
Since you are right joining domains to domains_data, the entries that are in domains_data are taken. If you want the entries of domains to be considered, use a left join like below.
SELECT d.id, d.domain_name, d.action, d.comment, d.agent_email, d.assigned_date, d.added_date, dd.registered_on, dd.expiry_date, dd.updated_date, dd.acquire_price, dd.acquire_date, dd.email, dd.effective_price, dd.registrar, dd.status, dd.servers, COUNT(l.lead_domain), d.domainer_email, d.current_status, d.undeveloped, d.sedo, d.afternic, d.flippa, d.uniregistry, d.go_daddy, d.domr, d.minimum_offer, d.buy_it_now_price, d.way_to_find_leads, dd.tlds_taken
FROM domains d
LEFT JOIN domains_data dd ON d.domain_name=dd.domain_name
LEFT OUTER JOIN lead_domains l ON d.domain_name=l.domain_name AND d.domainer_email=l.domainer_email
GROUP BY d.domain_name
HAVING d.action='all' AND d.domainer_email='abc#gmail.com'
ORDER BY d.added_date DESC;
If you are still not able to get the desired results, check the having conditions.
#Harshal As per my understanding, I don't think that query you have provided will work as GROUP BY rules are not followed properly.
SELECT d.id,
d.domain_name,
d.action,
d.comment,
d.agent_email,
d.assigned_date,
d.added_date,
dd.registered_on,
dd.expiry_date,
dd.updated_date,
dd.acquire_price,
dd.acquire_date,
dd.email,
dd.effective_price,
dd.registrar,
dd.status,
dd.servers,
(SELECT COUNT(lead_domain) FROM lead_domains WHERE domain_name = d.domain_name AND domainer_email = d.domainer_email) AS lead_domain,
d.domainer_email,
d.current_status,
d.undeveloped,
d.sedo,
d.afternic,
d.flippa,
d.uniregistry,
d.go_daddy,
d.domr,
d.minimum_offer,
d.buy_it_now_price,
d.way_to_find_leads,
dd.tlds_taken
FROM domains d
LEFT JOIN domains_data dd
ON d.domain_name = dd.domain_name
WHERE d.action = 'all'
AND d.domainer_email = 'abc#gmail.com'
ORDER BY
d.added_date DESC;
And as you have not provided any expected results, I am just guessing that my solution will work.
Here's my suggestion, filter first before joining other tables.
SELECT d.id
,d.domain_name
,d.action
,d.comment
,d.agent_email
,d.assigned_date
,d.added_date
,dd.registered_on
,dd.expiry_date
,dd.updated_date
,dd.acquire_price
,dd.acquire_date
,dd.email
,dd.effective_price
,dd.registrar
,dd.status
,dd.servers
,t1.cnt
,d.domainer_email
,d.current_status
,d.undeveloped
,d.sedo
,d.afternic
,d.flippa
,d.uniregistry
,d.go_daddy
,d.domr
,d.minimum_offer
,d.buy_it_now_price
,d.way_to_find_leads
,dd.tlds_taken
FROM domains d
inner join
(select domain_name, count(1) as cnt
from domain
where
action='all' and domainer_email='abc#gmail.com'
group by domain_name
) as t1 on t1.domain_name = d.domain_name
right join domains_data dd on d.domain_name=dd.domain_name
left outer join lead_domains l on d.domain_name=l.domain_name and d.domainer_email=l.domainer_email
order by d.added_date desc;

Get unique rows from one of tables in INNER JOIN

Is there any workaround to get unique rows which are in zc_orders table? I only need comments field from zc_zc_orders_status_history (which have duplicate rows). This is my query:
SELECT
zc_orders.orders_id,
zc_orders_status_history.comments,
zc_orders_status_history.orders_status_id,
zc_customers.customers_email_address,
zc_customers.customers_telephone,
zc_customers.customers_firstname,
zc_customers.customers_lastname,
zc_orders_status_history.date_added,
zc_orders.date_purchased
FROM
zc_customers
INNER JOIN zc_orders ON zc_orders.customers_id = zc_customers.customers_id
INNER JOIN zc_orders_status_history ON
zc_orders_status_history.orders_id = zc_orders.orders_id
where zc_orders_status_history.orders_status_id = 8
ORDER BY zc_orders_status_history.date_added DESC
SELECT DISTINCT
zc_orders_status_history.comments,
FROM
zc_customers
INNER JOIN zc_orders ON zc_orders.customers_id = zc_customers.customers_id
INNER JOIN zc_orders_status_history ON
zc_orders_status_history.orders_id = zc_orders.orders_id
where zc_orders_status_history.orders_status_id = 8
ORDER BY zc_orders_status_history.date_added DESC

MySQL left join 3 tables error

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.

MySQL query error 1064 (syntax error)

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.

mysql several count and join returns strange value

I'm trying to make a count within several table with JOIN, but when I made several JOINs the COUNTs got wrongly counted.
Basically I've got 4 tables, named:
predective_search
predective_to_product
predective_to_category
predective_to_manufacturer
I want to count the total number of products, categories and manufacturer which has same id in table predective_search.
Here's my code:
SELECT * ,
COUNT(pp.predictive_id) AS total_products,
COUNT(pc.predictive_id) AS total_categories,
COUNT(pm.predictive_id) AS total_manufacturers
FROM predictive_search ps
LEFT JOIN predictive_to_product pp ON (ps.predictive_id = pp.predictive_id)
LEFT JOIN predictive_to_category pu ON (ps.predictive_id = pc.predictive_id)
LEFT JOIN oc_predictive_to_manufacturer pm ON (ps.predictive_id = pm.predictive_id)
GROUP BY ps.predictive_id
Also the GROUP BY is needed I think. I'm stuck at this as I'm not getting any way to do this
SELECT
ps.*,
agg_pp.total_products,
agg_pc.total_categories,
agg_pm.total_manufacturers
FROM predictive_search ps
LEFT JOIN (
SELECT pp.predictive_id, COUNT(*) AS total_products
FROM predictive_to_product pp
GROUP BY pp.predictive_id
) agg_pp ON ps.predictive_id = agg_pp.predictive_id
LEFT JOIN (
SELECT pc.predictive_id, COUNT(*) AS total_categories
FROM predictive_to_category pc
GROUP BY pc.predictive_id
) agg_pc ON ps.predictive_id = agg_pc.predictive_id
LEFT JOIN (
SELECT pm.predictive_id, COUNT(*) AS total_manufacturers
FROM predictive_to_category pm
GROUP BY pm.predictive_id
) agg_pm ON ps.predictive_id = agg_pm.predictive_id