我正在编写SQL查询,但我收到错误
您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以获得正确的语法,以便在t.status附近使用 限制0,25和#39;在第1行。
然而,如果没有分组,它可以正常工作,有些人可以帮我解决这个问题。
这是查询(小一点: - ))
SELECT
count(t.status) as counts,
(
SELECT attribute_values
FROM product_attribute_values pav
WHERE attr_name = 'Brand'
AND contract_id = c.id
AND product_id = c.product
AND pav.sp_id = c.sp_id
) as make_brand,
(
SELECT group_concat(sc.short_name)
FROM contract_product_service_centers cpsc
LEFT JOIN service_centers sc ON cpsc.service_center_id = sc.id AND cpsc.sp_id = sc.sp_id
WHERE sc.sp_id = c.sp_id
AND cpsc.contract_id = c.contract_id
AND cpsc.service_center_id IN (1, 14, 3, 4)
) as service_center_name,
(
SELECT attribute_values
FROM product_attribute_values pav1
WHERE attr_name='Model'
and contract_id=c.id
and product_id=c.product
and pav1.sp_id=c.sp_id
) as model,
(
SELECT supervisor_id
FROM `user_links` ul, contract_product_service_centers cpsc
where ul.service_center_id=cpsc.service_center_id
and ul.sp_id=cpsc.sp_id
and ul.sp_id=cpsc.sp_id
and ul.category_id=c.category
and ul.sp_id=c.sp_id
and cpsc.contract_id=c.contract_id
) as linked_sup,assigned_sup_id,
assigned_se_id,
assigned_by,
date_format(t.datecreated, '%d-%b-%Y %I:%i %p') as created_date,
date_format(closeddate, '%d-%b-%Y %I:%i %p') as closed_date,
date_format(assigned_datetime, '%d-%b-%Y %I:%i %p') as tkt_assigned_datetime,
datediff(current_Date, date(t.datecreated)) as days_since_tkt_created,
p.ct_template_id,
(
select ct_f.name
from forms ct_f
where ct_f.id=p.ct_template_id
) as ct_template_name,
p.mt_template_id,
(
select mt_f.name
from forms mt_f
where mt_f.id=p.mt_template_id
) as mt_template_name
FROM tickets t
left join contracts c on t.contract_id=c.contract_id and t.sub_contract_no=c.sub_contract_no and t.sp_id=c.sp_id
left join products p on c.product=p.id left join product_categories pc on c.category=pc.id and p.category_id=pc.id
where t.status in (2,5,6)
and t.sp_id='VIV74'
and c.category in (1,12,2)
and c.contract_id in ('CN10001')
and ( t.ticket_id='CT100001')
and ( t.ticket_display_status='Accept Closure')
and ( p.product_short_name ='LAP')
and ( pc.short_name ='ELE')
and ( firstname ='Rajesh Kumar')
and ( c.contract_id='CN10001')
having service_center_name in ('SCONE','SC-01')
and days_since_tkt_created in ('44','43','39')
and make_brand in ('lg','samsung')
and model in ('23','46')
group by t.status
答案 0 :(得分:0)
having
子句应该在group by
子句之后。
当然,拥有这么大的代码墙并不容易调试它,但这是在编辑你的问题时在我眼前突然出现的第一件事。
SELECT
count(t.status) as counts,
(
SELECT attribute_values
FROM product_attribute_values pav
WHERE attr_name = 'Brand'
AND contract_id = c.id
AND product_id = c.product
AND pav.sp_id = c.sp_id
) as make_brand,
(
SELECT group_concat(sc.short_name)
FROM contract_product_service_centers cpsc
LEFT JOIN service_centers sc ON cpsc.service_center_id = sc.id AND cpsc.sp_id = sc.sp_id
WHERE sc.sp_id = c.sp_id
AND cpsc.contract_id = c.contract_id
AND cpsc.service_center_id IN (1, 14, 3, 4)
) as service_center_name,
(
SELECT attribute_values
FROM product_attribute_values pav1
WHERE attr_name='Model'
and contract_id=c.id
and product_id=c.product
and pav1.sp_id=c.sp_id
) as model,
(
SELECT supervisor_id
FROM `user_links` ul, contract_product_service_centers cpsc
where ul.service_center_id=cpsc.service_center_id
and ul.sp_id=cpsc.sp_id
and ul.sp_id=cpsc.sp_id
and ul.category_id=c.category
and ul.sp_id=c.sp_id
and cpsc.contract_id=c.contract_id
) as linked_sup,assigned_sup_id,
assigned_se_id,
assigned_by,
date_format(t.datecreated, '%d-%b-%Y %I:%i %p') as created_date,
date_format(closeddate, '%d-%b-%Y %I:%i %p') as closed_date,
date_format(assigned_datetime, '%d-%b-%Y %I:%i %p') as tkt_assigned_datetime,
datediff(current_Date, date(t.datecreated)) as days_since_tkt_created,
p.ct_template_id,
(
select ct_f.name
from forms ct_f
where ct_f.id=p.ct_template_id
) as ct_template_name,
p.mt_template_id,
(
select mt_f.name
from forms mt_f
where mt_f.id=p.mt_template_id
) as mt_template_name
FROM tickets t
left join contracts c on t.contract_id=c.contract_id and t.sub_contract_no=c.sub_contract_no and t.sp_id=c.sp_id
left join products p on c.product=p.id left join product_categories pc on c.category=pc.id and p.category_id=pc.id
where t.status in (2,5,6)
and t.sp_id='VIV74'
and c.category in (1,12,2)
and c.contract_id in ('CN10001')
and ( t.ticket_id='CT100001')
and ( t.ticket_display_status='Accept Closure')
and ( p.product_short_name ='LAP')
and ( pc.short_name ='ELE')
and ( firstname ='Rajesh Kumar')
and ( c.contract_id='CN10001')
group by t.status
having service_center_name in ('SCONE','SC-01')
and days_since_tkt_created in ('44','43','39')
and make_brand in ('lg','samsung')
and model in ('23','46')