whats wrong with this query when i run they got a syntax error
INSERT IGNORE INTO an_catalogsearch_fulltext (product_id, store_id, data_index)
SELECT DISTINCT ca_ent.entity_id as product_id,
4 as store_id,
CONCAT(
ifnull(ca_var.value,''),' ',
ifnull(ca_ent.sku,''), ' ',
ifnull(
(
SELECT GROUP_CONCAT( ca_ent2.sku) FROM `an_catalog_product_entity` ca_ent2 LEFT JOIN `an_catalog_product_super_link` ca_sup
ON ca_sup.product_id = ca_ent2.entity_id WHERE ca_sup.parent_id = ca_ent.entity_id GROUP BY ca_sup.parent_id
),'') ,' ',
ifnull(
(
SELECT GROUP_CONCAT( ca_ent3.sku) FROM `an_catalog_product_entity` ca_ent3 LEFT JOIN `an_catalog_product_link` ca_lin
ON ca_lin.linked_product_id = ca_ent3.entity_id WHERE ca_lin.product_id = ca_ent.entity_id GROUP BY ca_lin.product_id
),'') ,' ',
ifnull(ca_text.value,'') , ' ',
ifnull(
select option_value.value from `an_eav_attribute_option_value` as option_value where option_value.option_id = an_alt_norm_form.value, '')
) as data_index FROM `an_catalog_product_entity` ca_ent
LEFT JOIN `an_catalog_product_entity_varchar` ca_var ON ca_var.entity_id = ca_ent.entity_id AND ca_var.attribute_id = 71
LEFT JOIN `an_catalog_product_entity_text` ca_text ON ca_text.entity_id = ca_ent.entity_id AND ca_text.attribute_id = 72
LEFT JOIN `an_catalog_product_entity_int` an_alt_norm_form ON an_alt_norm_form.entity_id = ca_ent.entity_id AND an_alt_norm_form.attribute_id = 306 ;
when i add
ifnull(select option_value.value from `an_eav_attribute_option_value` as option_value where option_value.option_id = an_alt_norm_form.value, '')
they give me a syntax error
when i replace this line with simple ifnull(an_alt_norm_form.value, '') working fine what i m doing wrong
Compare the syntax you used to the already existing ifnull statements:
ifnull(
(
SELECT GROUP_CONCAT( ca_ent2.sku) FROM `an_catalog_product_entity` ca_ent2 LEFT JOIN `an_catalog_product_super_link` ca_sup
ON ca_sup.product_id = ca_ent2.entity_id WHERE ca_sup.parent_id = ca_ent.entity_id GROUP BY ca_sup.parent_id
),'')
here you wrapped the subquery in an additional set of parenthesis, but with your newly added
ifnull(select option_value.value from `an_eav_attribute_option_value` ..., '')
those are missing.
INSERT IGNORE INTO an_catalogsearch_fulltext (product_id, store_id, data_index)
SELECT DISTINCT ca_ent.entity_id as product_id,
4 as store_id,
CONCAT(
ifnull(ca_var.value,''),' ',
ifnull(ca_ent.sku,''), ' ',
IFNULL((SELECT GROUP_CONCAT( ca_ent2.sku) FROM `an_catalog_product_entity` ca_ent2 LEFT JOIN `an_catalog_product_super_link` ca_sup
ON ca_sup.product_id = ca_ent2.entity_id WHERE ca_sup.parent_id = ca_ent.entity_id GROUP BY ca_sup.parent_id
),'') ,' ',
IFNULL((SELECT GROUP_CONCAT( ca_ent3.sku) FROM `an_catalog_product_entity` ca_ent3 LEFT JOIN `an_catalog_product_link` ca_lin
ON ca_lin.linked_product_id = ca_ent3.entity_id WHERE ca_lin.product_id = ca_ent.entity_id GROUP BY ca_lin.product_id),'') ,' ',
ifnull(ca_text.value,'') , ' ',
IFNULL((select option_value.value from `an_eav_attribute_option_value` as option_value where option_value.option_id = an_alt_norm_form.value),'') ,' ') as data_index FROM `an_catalog_product_entity` ca_ent
LEFT JOIN `an_catalog_product_entity_varchar` ca_var ON ca_var.entity_id = ca_ent.entity_id AND ca_var.attribute_id = 71
LEFT JOIN `an_catalog_product_entity_text` ca_text ON ca_text.entity_id = ca_ent.entity_id AND ca_text.attribute_id = 72
LEFT JOIN `an_catalog_product_entity_int` an_alt_norm_form ON an_alt_norm_form.entity_id = ca_ent.entity_id AND an_alt_norm_form.attribute_id = 306 ;
Try above query.
Related
If I run this query directly in PHPMyAdmin, it returns 13420 rows in 0.2091 second, but if I run the exact same query as a stored procedure, it returns the same amount of row but it takes forever and sometimes the SQL server returns an out of memory exception.
I'm at a total loss - any advice would be welcome, because I can't work out why this slows everything down?!
SELECT
el.UID as LUID,
se.UID as DUID,
el.event_title,
el.event_synopsis,
se.behind_the_scenes,
se.sub_event_title,
se.event_eventDateAndTime,
se.event_eventDateAndTimeEnd,
el.event_confirmed,
el.event_active,
(
SELECT GROUP_CONCAT(sp2.color SEPARATOR ',')
FROM setup__spaces sp2
LEFT JOIN events__assigned_spaces eas2 ON ( eas2.space_id = sp2.UID )
LEFT JOIN events__events_list el2 ON (el2.UID = eas2.event_id)
WHERE el2.UID = el.UID
) as spaceColors,
(
SELECT GROUP_CONCAT(sp2.name SEPARATOR ', ')
FROM setup__spaces sp2
LEFT JOIN events__assigned_spaces eas2 ON ( eas2.space_id = sp2.UID )
LEFT JOIN events__events_list el2 ON (el2.UID = eas2.event_id)
WHERE el2.UID = el.UID
) as spaceNames,
(
SELECT GROUP_CONCAT(sp2.UID SEPARATOR ',')
FROM setup__spaces sp2
LEFT JOIN events__assigned_spaces eas2 ON ( eas2.space_id = sp2.UID )
LEFT JOIN events__events_list el2 ON (el2.UID = eas2.event_id)
WHERE el2.UID = el.UID
) as spaceIds,
(
SELECT GROUP_CONCAT(t.UID SEPARATOR ',')
FROM setup__tags t
LEFT JOIN events__assigned_tags eat ON ( eat.tag_id = t.UID )
LEFT JOIN events__events_list el2 ON (el2.UID = eat.event_id)
WHERE el2.UID = el.UID
) as tagIds,
(
SELECT GROUP_CONCAT(t.tag_name SEPARATOR ', ')
FROM setup__tags t
LEFT JOIN events__assigned_tags eat ON ( eat.tag_id = t.UID )
LEFT JOIN events__events_list el2 ON (el2.UID = eat.event_id)
WHERE el2.UID = el.UID
) as tagNames
FROM events__events_list el
INNER JOIN events__sub_events se ON (el.UID = se.event_masterEvent)
WHERE ((el.event_active='1') OR (el.event_active='0' AND el.event_confirmed = '1'))
AND el.company_uid = sp_company_uid
With thanks to #Akina for pointing me in the right direction, I found help from the following places:
stackoverflow.com - Selecting multiple columns/fields in MySQL subquery
geeksengine.com - How to use subquery in JOIN operation in MySQL
Here's the revised code that's now lightning fast!
SELECT
el.UID as LUID,
se.UID as DUID,
el.event_title,
el.event_synopsis,
se.behind_the_scenes,
se.sub_event_title,
se.event_eventDateAndTime,
se.event_eventDateAndTimeEnd,
el.event_confirmed,
el.event_active,
tags.names as tagNames,
tags.ids as tagIds,
spaces.names as spaceNames,
spaces.colors as spaceColors,
spaces.ids as spaceIds
FROM events__events_list el
INNER JOIN events__sub_events se ON (el.UID = se.event_masterEvent)
LEFT JOIN (
SELECT
el3.UID as el2uid,
GROUP_CONCAT(s.name SEPARATOR ', ') as names,
GROUP_CONCAT(s.color SEPARATOR ',') as colors,
GROUP_CONCAT(s.UID SEPARATOR ',') as ids
FROM setup__spaces as s
LEFT JOIN events__assigned_spaces eas ON ( eas.space_id = s.UID )
LEFT JOIN events__events_list el3 ON ( el3.UID = eas.event_id )
GROUP BY el3.UID
) as spaces on spaces.el2uid = el.UID
LEFT JOIN (
SELECT
el2.UID as el2uid,
GROUP_CONCAT(t.tag_name SEPARATOR ', ') as names,
GROUP_CONCAT(t.UID SEPARATOR ',') as ids
FROM setup__tags as t
LEFT JOIN events__assigned_tags eat ON ( eat.tag_id = t.UID )
LEFT JOIN events__events_list el2 ON ( el2.UID = eat.event_id )
GROUP BY el2.UID
) as tags on tags.el2uid = el.UID
WHERE ((el.event_active='1') OR (el.event_active='0' AND el.event_confirmed = '1'))
AND el.company_uid = sp_company_uid
I get the names twice and I need them to appear only once instead. How can I fix it?
I am using the SQL SELECT, stated below:
SELECT
( LTRIM(SUBSTRING(resource.name, CHARINDEX(',', resource.name) + 1, LENGTH(resource.name) - CHARINDEX(',', resource.name)))) AS firstname,
(SELECT GROUP_CONCAT(SUBSTRING(REPLACE(resource.name, '*Deleted*', '') FROM 1 FOR POSITION(',' IN REPLACE(resource.name, '*Deleted*', '' ))-1))) AS lastname,
(SELECT GROUP_CONCAT(obj SEPARATOR ', ') FROM rel_raci resource_raci_r WHERE resource_raci_r.PERSON_ID = resource.id AND resource_raci_r.RACI ='R' AND getOrgtype(obj_id) = 6 ) AS companycode,
(SELECT GROUP_CONCAT(REPLACE(obj, '*Deleted*', '')) FROM rel_raci resource_raci_r WHERE resource_raci_r.PERSON_ID = resource.id AND resource_raci_r.RACI ='R' AND getOrgtype(obj_id) = 4 ) AS organizationalunit,
(SELECT GROUP_CONCAT(obj SEPARATOR ', ') FROM rel_raci resource_raci_r WHERE resource_raci_r.RACI ='R' ) AS responsible,
resource.identifier AS identifier,
resource.phone AS phone,
organisation.keywords AS keywords,
resource.keywords AS persno,
resource.id AS obj_id,
resource.mobile AS mobile,
resource.e_mail AS email,
resource.alias AS alias,
resource.city AS city,
resource.postcode AS postcode,
resource.state AS state,
resource.street AS street,
resource.country AS country
FROM obj_resource resource
LEFT OUTER JOIN rel_raci resource_raci ON resource.ID = resource_raci.PERSON_ID
LEFT OUTER JOIN obj_resource organisation on organisation.ID = resource_raci.OBJ_ID
-- Gibt nur die markierten Massnahmen aus
WHERE CONTAINS($P{TE_SELECTIONS}, resource.id, -1)
Problem:
I know it's documented in MYSQL DOCUMENTS That in where clause "alias" cannot be used since where clause is not populated yet. But i have to get the data matched from other table values in where clause (IN condition).
I have also read some similar kind of post but this one is quite different & big complex one. I couldn't make it working with my efforts of last 3 days.
It's showing error (excepted as per documentation)
UNKNOWN COLUMN "Ind_ID" in WHERE CLAUSE
I have to match similarly for FA_ID & PREFERRED_LOCATION_ID field
Select a.job_id, a.Employer_ID, a.Sub_user_id,
Date_format(a.creation_on,'%d-%m-%Y') as Created_date, a.Job_type,
a.Designation, a.Open_Positions, a.Job_Description, a.Min_age,
a.Max_age, a.min_exp, a.max_exp, a.Hide_Salary, a.company_name,
a.About_Company, a.Contact_person_name, a.Contact_No, a.Refresh_type,
a.Response_type,
(Select GROUP_CONCAT(DISTINCT g.Education ORDER BY pjedu.Education_ID
SEPARATOR ', ') user_education
from e_pj_edu pjedu
INNER JOIN education g ON FIND_IN_SET(g.Edu_ID, pjedu.Education_ID)
where a.job_id = pjedu.Job_ID
) as Education_ID,
(Select GROUP_CONCAT(DISTINCT h.FA_description ORDER BY uf.FA_ID
SEPARATOR ', ') FA
from e_pj_fa uf
INNER JOIN functional_area h ON FIND_IN_SET(h.FA_ID, uf.FA_ID)
where a.Job_ID = uf.Job_ID
) as FA_ID,
(Select GROUP_CONCAT(DISTINCT i.Industry_description ORDER BY
ui.Industry_ID SEPARATOR ', ') Industry_ID
from e_pj_industry ui
INNER JOIN industry i ON FIND_IN_SET(i.Industry_ID, ui.Industry_ID)
where a.Job_ID = ui.Job_ID
) as Ind_ID,
(Select GROUP_CONCAT(DISTINCT j.location_name ORDER BY
upl.Location_ID SEPARATOR ', ') Location_ID
from e_pj_locations upl
INNER JOIN locations j ON FIND_IN_SET(j.location_id, upl.Location_ID)
where a.Job_ID = upl.Job_ID
) as Preferred_Location_ID,
(Select GROUP_CONCAT(DISTINCT uk.Keyword_Name ORDER BY uk.Keyword_ID
SEPARATOR ', ') keyskills
from e_pj_keywords uk
where a.Job_ID = uk.Job_ID
) as Keyword_Name,
GROUP_CONCAT(DISTINCT cc.salary_description ORDER BY cc.salary_ID
SEPARATOR ', ') Min_salary,
GROUP_CONCAT(DISTINCT dd.salary_description ORDER BY dd.salary_ID
SEPARATOR ', ') Max_salary
from post_jobs a
INNER JOIN user_salary cc ON FIND_IN_SET(cc.salary_ID, a.Min_salary)
INNER JOIN user_salary dd ON FIND_IN_SET(dd.salary_ID, a.Max_salary)
WHERE a.Designation LIKE '%MIS%' or a.company_name LIKE '%MIS%'
And a.max_exp <= 9
And a.Max_salary<=110
And Ind_ID IN (10001,10002,10004)
And FA_ID IN(1001)
group by a.job_id
First thing that comes in my mind is just move your aliased where conditions to outer query, i.e.:
select * from (
Select a.job_id ....
WHERE a.Designation LIKE '%MIS%' or a.company_name LIKE '%MIS%'
And a.max_exp <= 9
And a.Max_salary<=110
group by a.job_id
) inner
where
Ind_ID IN (10001,10002,10004)
And FA_ID IN(1001)
GL!
Posting revised query which worked. May be this helps someone from wasting 3 days like me :-)
select * from (
Select a.job_id, a.Employer_ID, a.Sub_user_id, Date_format(a.creation_on,'%d-%m-%Y') as Created_date, a.Job_type, a.Designation, a.
Open_Positions, a.Job_Description, a.Min_age, a.Max_age, a.min_exp, a.max_exp, a.Hide_Salary, a.company_name, a.About_Company, a.Contact_person_name,
a.Contact_No, a.Refresh_type, a.Response_type,
(Select GROUP_CONCAT(DISTINCT g.Education ORDER BY pjedu.Education_ID SEPARATOR ', ') user_education
from e_pj_edu pjedu
INNER JOIN education g ON FIND_IN_SET(g.Edu_ID, pjedu.Education_ID)
where a.job_id = pjedu.Job_ID
) as Education_ID,
(Select GROUP_CONCAT(DISTINCT h.FA_description ORDER BY uf.FA_ID SEPARATOR ', ') FA
from e_pj_fa uf
INNER JOIN functional_area h ON FIND_IN_SET(h.FA_ID, uf.FA_ID)
where a.Job_ID = uf.Job_ID
) as FA_ID,
(Select GROUP_CONCAT(DISTINCT i.Industry_description ORDER BY ui.Industry_ID SEPARATOR ', ') Industry_ID
from e_pj_industry ui
INNER JOIN industry i ON FIND_IN_SET(i.Industry_ID, ui.Industry_ID)
where a.Job_ID = ui.Job_ID
) as Ind_ID,
(Select GROUP_CONCAT(DISTINCT j.location_name ORDER BY upl.Location_ID SEPARATOR ', ') Location_ID
from e_pj_locations upl
INNER JOIN locations j ON FIND_IN_SET(j.location_id, upl.Location_ID)
where a.Job_ID = upl.Job_ID
) as Preferred_Location_ID,
(Select GROUP_CONCAT(DISTINCT uk.Keyword_Name ORDER BY uk.Keyword_ID SEPARATOR ', ') keyskills
from e_pj_keywords uk
where a.Job_ID = uk.Job_ID
) as Keyword_Name,
GROUP_CONCAT(DISTINCT cc.salary_description ORDER BY cc.salary_ID SEPARATOR ', ') Min_salary,
GROUP_CONCAT(DISTINCT dd.salary_description ORDER BY dd.salary_ID SEPARATOR ', ') Max_salary
from post_jobs a
INNER JOIN user_salary cc ON FIND_IN_SET(cc.salary_ID, a.Min_salary)
INNER JOIN user_salary dd ON FIND_IN_SET(dd.salary_ID, a.Max_salary)
group by a.Job_id
) aa
WHERE Designation LIKE '%op%' or company_name LIKE '%op%'
And max_exp <= 15
And Max_salary<=120
and Ind_ID IN (10001,10002,10004,10003)
And FA_ID IN(1001,1002,1003)
group by Job_id
Here's my code:
$queryresult="SELECT a.ArticleID, a.Title, a.Publication, l.EnglishLanguage, m.EnglishMedia, a.PublicationYear, a.Size , REPLACE( GROUP_CONCAT( DISTINCT REPLACE( c.EnglishCategory, ',', '' ) ) , '/', ',' ) , GROUP_CONCAT( DISTINCT au.EnglishName, '/' ), GROUP_CONCAT( DISTINCT au.PictureURL ) , a.Abstract,a.URL,m.IconURL,REPLACE( GROUP_CONCAT( DISTINCT REPLACE( au.EnglishName, ',', '' ) ) , '/', ',' )
FROM article a
LEFT JOIN aritcletocategory ac ON a.ArticleID = ac.ArticleID
LEFT JOIN category c ON ac.CategoryID = c.CategoryID
LEFT JOIN articletoauthor at ON a.ArticleID = at.ArticleID
JOIN MediaType m ON a.MediaTypeID = m.MediaTypeID
JOIN Language l ON a.LanguageID = l.LanguageID
LEFT JOIN author au ON at.AuthorID = au.AuthorID
WHERE a.Title REGEXP '".$search."' || replace(au.EnglishName,',','') REGEXP '".$search."' || replace(au.HebrewName,',','') REGEXP '".$search."' || a.Keywords REGEXP '".$search."'|| c.EnglishCategory REGEXP '".$search."' || a.Abstract REGEXP '".$search."'|| a.Publication REGEXP '".$search."'GROUP BY a.ArticleID";
$finalresult=show_result($queryresult);
And the Id I got from result of this query is used in the following query:
foreach($finalresult as $ress){
$queryresults="SELECT a.ArticleID, a.Title, a.Publication, l.EnglishLanguage, m.EnglishMedia, a.PublicationYear, a.Size , REPLACE( GROUP_CONCAT( DISTINCT REPLACE( c.EnglishCategory, ',', '' ) ) , '/', ',' ) , GROUP_CONCAT( DISTINCT au.EnglishName , '/' ), GROUP_CONCAT( au.PictureURL ) , a.Abstract,a.URL, REPLACE( GROUP_CONCAT( DISTINCT REPLACE( au.HebrewName, ',', '' ) ) , '/', ',' ),m.IconURL,REPLACE( GROUP_CONCAT( DISTINCT REPLACE( au.EnglishName, ',', '' ) ) , '/', ',' )
FROM article a
LEFT JOIN aritcletocategory ac ON a.ArticleID = ac.ArticleID
LEFT JOIN category c ON ac.CategoryID = c.CategoryID
LEFT JOIN articletoauthor at ON a.ArticleID = at.ArticleID
LEFT JOIN author au ON at.AuthorID = au.AuthorID
JOIN MediaType m ON a.MediaTypeID = m.MediaTypeID
JOIN Language l ON a.LanguageID = l.LanguageID
WHERE a.ArticleID =".$ress[0]." GROUP BY a.ArticleID ";
$finalresults=show_result($queryresults);
I wish to get the result with one query rather than using two queries. How can I do that?
As your queries have the same structure you can use UNION
--Query 1
SELECT ...
UNION
--Query 2
SELECT ...
;
Check out w3schools - SQL UNION Operator for more information.
Or if you want to get more professional information: MySQL Manual: Union Syntax
I'm trying to show the boroughs and postcodes a particular town in is.
My database is fairly well structured, with a table such as town, postcode and borough. There are also tables for each of the relationships town_postcode & town_borough.
Ideally I want the data returned as:
"Abbey Wood", "SE2", "Bexley, Greenwich"
"Barbican", "EC1, EC2", "City of London"
I've tried a few different approaches and I'm close but not there yet.
Any help would be appreciated... :)
So far I've tried
SELECT DISTINCT t.town,
GROUP_CONCAT( DISTINCT p.postcode SEPARATOR ', ' ) AS 'postcode',
GROUP_CONCAT( DISTINCT b.borough SEPARATOR ', ' ) AS 'borough'
FROM coverage_towns AS t,
coverage_boroughs AS b,
coverage_postcodes AS p,
coverage_towns_boroughs AS tb,
coverage_towns_postcodes AS tp
WHERE t.id = tp.town_id
AND p.id = tp.postcode_id
AND b.id = tb.borough_id
GROUP BY t.town
ORDER BY t.town ASC
Which returns
"Abbey Wood", "SE2", "Southwark, Hammersmith and Fulham, Tower Hamlets, Wandsworth, Enfield, Newham, LOTS MORE HERE"
"Barbican", "EC1, EC2", "Brent, Greenwich, Kensington and Chelsea, Westminster, Camden, LOTS MORE HERE"
I've also tried
SELECT DISTINCT t.town, (
SELECT SQL_CACHE DISTINCT GROUP_CONCAT( p1.postcode
SEPARATOR ', ' )
FROM coverage_postcodes AS p1
WHERE p1.id = tp.postcode_id
) AS 'postcode', (
SELECT SQL_CACHE DISTINCT GROUP_CONCAT( b1.borough
SEPARATOR ', ' )
FROM coverage_boroughs AS b1
WHERE b1.id = tb.borough_id
) AS 'borough'
FROM coverage_towns AS t, coverage_boroughs AS b, coverage_postcodes AS p, coverage_towns_boroughs AS tb, coverage_towns_postcodes AS tp
WHERE t.id = tp.town_id
AND p.id = tp.postcode_id
AND b.id = tb.borough_id
GROUP BY t.town
ORDER BY t.town ASC
Which returns
"Abbey Wood", "SE2", "Greenwich"
"Acton", "W3", "Greenwich"
"Aldersbrook", "E12", "Greenwich"
First query looks good, just add distinct inside the group_concat, like:
SELECT t.town
, GROUP_CONCAT(DISTINCT p.postcode SEPARATOR ', ' ) AS 'postcode'
, GROUP_CONCAT(DISTINCT b.borough SEPARATOR ', ' ) AS 'borough'
<more code here>
GROUP BY
t.town
SOLUTION
I came back to the question after a good coffee and the answer presented itself.
SELECT DISTINCT t.town,
GROUP_CONCAT( DISTINCT p.postcode SEPARATOR ', ' ) AS 'postcode',
GROUP_CONCAT( DISTINCT b.borough SEPARATOR ', ' ) AS 'borough'
FROM towns AS t, boroughs AS b, postcodes AS p, towns_boroughs AS tb, towns_postcodes AS tp
WHERE (t.id = tp.town_id AND t.id = tb.town_id)
AND (p.id = tp.postcode_id AND b.id = tb.borough_id)
GROUP BY t.town
ORDER BY t.town ASC