Merging two queries into One - mysql

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

Related

how to use the column in a temporary table for the following update in MSQL

I have a MySQL database.
I want to update a column(in my case title column in bms_title table) in a table using the values from concat columns in other tables.
SELECT * FROM(SELECT distinct t.id, t.title as Textbook,
GROUP_CONCAT(concat(ci.discipline_code, ci.code, " (" , ci.type , ")") SEPARATOR ', ') as CourseCode FROM
tms_local.bms_material m,
tms_local.bms_title t,
tms_local.bms_course c,
tms_local.bms_courseinfo ci
where t.id > 1 AND t.id = m.book_id
and c.id = m.course_id
and ci.id = c.id
and isbn != 'NA'
GROUP BY t.id) AS temporary_table;
UPDATE tms_local.bms_title
SET tms_local.bms_title.thumbnail = temporary_table.CourseCode
WHERE tms_local.bms_title.title=temporary_table.Textbook;
But I got the error: Unknow temporary_table.Textbook in where clause.
How could I update the tms_local.bms_title.thumbnail column using CourseCode column from the selected table?
enter image description here
I have tried
CREATE TEMPORARY TABLE IF NOT EXISTS temporary_table AS (SELECT distinct t.id, t.title as Textbook,
GROUP_CONCAT(concat(ci.discipline_code, ci.code, " (" , ci.type , ")") SEPARATOR ', ') as CourseCode FROM
tms_local.bms_material m,
tms_local.bms_title t,
tms_local.bms_course c,
tms_local.bms_courseinfo ci
where t.id > 1 AND t.id = m.book_id
and c.id = m.course_id
and ci.id = c.id
and isbn != 'NA'
GROUP BY t.id);
UPDATE tms_local.bms_title
SET tms_local.bms_title.thumbnail = temporary_table.CourseCode
WHERE tms_local.bms_title.title=temporary_table.Textbook;
But got the same error.
you need to join the select statement.
As seen below:
UPDATE tms_local.bms_title t0
INNER JOIN
(SELECT
*
FROM
(SELECT DISTINCT
t.id,
t.title AS Textbook,
GROUP_CONCAT(CONCAT(ci.discipline_code, ci.code, ' (', ci.type, ')')
SEPARATOR ', ') AS CourseCode
FROM
tms_local.bms_material m, tms_local.bms_title t, tms_local.bms_course c, tms_local.bms_courseinfo ci
WHERE
t.id > 1 AND t.id = m.book_id
AND c.id = m.course_id
AND ci.id = c.id
AND isbn != 'NA'
GROUP BY t.id) AS temporary_table) t1 ON t0.title = t1.Textbook
SET
t0.thumbnail = t1.ourseCode;
Your temporary_table is being lost between the first statement and the second.
I find the WITH ... AS SQL structure to be helpful to get these together and is far more readable:
WITH temporary_table AS(
SELECT *
FROM(SELECT distinct t.id,
t.title as Textbook,
GROUP_CONCAT(concat(ci.discipline_code,
ci.code,
" (" ,
ci.type ,
")")
SEPARATOR ', '
) as CourseCode
FROM tms_local.bms_material m,
tms_local.bms_title t,
tms_local.bms_course c,
tms_local.bms_courseinfo ci
WHERE t.id > 1 AND t.id = m.book_id
and c.id = m.course_id
and ci.id = c.id
and isbn != 'NA'
GROUP BY t.id)
UPDATE tms_local.bms_title
SET tms_local.bms_title.thumbnail = temporary_table.CourseCode
WHERE tms_local.bms_title.title=temporary_table.Textbook;

Whats wrong I get all the names twice?

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)

mysql syntax error on sub query in insert values

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.

unknown column in IN/ALL/ANY subquery where clause

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

mysql multiple-subquery group_concat query

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