hi I want to put an alias in my join statement.
my code is:
SELECT distinct id_no, '' as picture, concat(lastname, IF(LENGTH(firstname) > 0 OR LENGTH(middlename) > 0, ', ', ''), concat(IF(p.degree = 5, 'Dr. ', IF(p.degree = 6, 'Dr. ', IF(p.degree = 28, 'Dr. ', '')))), firstname, IF(LENGTH(middlename) > 0, CONCAT(' ', middlename), '')) as name, tp.profession, '' as cv, e.entry_date, e.termination_date
FROM (bims_people as p)
LEFT JOIN bims_people_education as bpe ON bpe.people_id = p.id_no
LEFT JOIN tcms_profession as tp ON tp.profession_id = bpe.profession
LEFT JOIN (select * from bims_people_employment_contract_data order by termination_date DESC) e ON e.people_id = p.id_no
WHERE p.marked = 0 AND p.id_no > 1 GROUP BY p.id_no ORDER BY lastname ASC LIMIT 100, 50 ;
and I want it this way:
SELECT distinct id_no, '' as picture, concat(lastname, IF(LENGTH(firstname) > 0 OR LENGTH(middlename) > 0, ', ', ''), concat(IF(p.degree = 5, 'Dr. ', IF(p.degree = 6, 'Dr. ', IF(p.degree = 28, 'Dr. ', '')))), firstname, IF(LENGTH(middlename) > 0, CONCAT(' ', middlename), '')) as name, tp.profession, '' as cv, e.entry_date, e.termination_date
FROM (bims_people as p)
LEFT JOIN bims_people_education as bpe ON bpe.people_id = p.id_no
LEFT JOIN tcms_profession as tp ON tp.profession_id = bpe.profession
LEFT JOIN (select * from bims_people_employment_contract_data **where people_id = p.id_no** order by termination_date DESC limit 1) e ON e.people_id = p.id_no
WHERE p.marked = 0 AND p.id_no > 1 GROUP BY p.id_no ORDER BY lastname ASC LIMIT 100, 50 ;
i keeps telling me that p.id_no is unknown;
LEFT JOIN (select * from bims_people_employment_contract_data
where people_id = p.id_no
order by termination_date DESC limit 1) e
ON e.people_id = p.id_no
You cannot use a table's alias (correlation name) to define another table to JOIN to it. After you have given two tables & aliases to JOIN, ON or WHERE can use the alias in conditions.
From MySQL 5.7 Reference Manual 14.2.10.8 Subqueries in the FROM Clause
:
Subqueries in the FROM clause cannot be correlated subqueries, unless used within the ON clause of a JOIN operation.
(What do you expect the quoted OUTER JOIN to do? Since p.id_no doesn't appear in an ON or WHERE, there's no particular row of a table p that it could be the id_no value of.)
Maybe you want:
LEFT JOIN (SELECT * FROM bims_people_employment_contract_data
WHERE (people_id, termination_date) IN
(SELECT people_id, MAX(termination_date) AS termination_date
FROM bims_people_employment_contract_data
GROUP BY people_id)
) e
ON e.people_id = p.id_no
Related
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;
I'm using MySQL. I have to separate multi repeated rows into columns. The table has following structure.
But I need like this.
Note: There will be always 6 records of each DateTime. But Title and Feedback are dynamic. I tried to use
select DateTime,
But this is not giving my expected value. I wrote a code (not considered about title):
select
g.dateTime AS dateTime,
g.feedback as feedback ,
c.title AS title
from gauge g
inner join category c on g.category_id=c.category_id AND c.title ='title' AND g.feedback ='feedback'
group by g.dateTime
This doesn't work and I tried GROUP_CONCAT(.... SEPARATOR'.....' ) this gives me not expected output, just gives output in one column. My approach might be wrong.
Use a dynamic pivot since there are many different values
Fiddle to play with
drop table t;
create table t
(
dateTime datetime,
title varchar(50),
feedback int
);
insert into t values
( '2018-06-29 12:55:36', 'A', 1),
( '2018-06-29 12:55:36', 'B', 2),
( '2018-06-22 12:55:36', 'A', 1),
( '2018-06-22 12:55:36', 'B', 2),
( '2018-06-22 12:55:36', 'C', 3);
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(CASE WHEN title = ''',
title,
''' THEN ''', title ,''' END) AS ',
CONCAT(' Title',title,',')
),
CONCAT(
'MAX(CASE WHEN feedback = ''',
feedback,
''' THEN ''', feedback ,''' END) AS ',
CONCAT(' Feedback',feedback)
)
) INTO #sql
FROM T;
SET #sql = CONCAT('SELECT dateTime, ', #sql, '
FROM t
GROUP BY dateTime');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
I would simply use conditional aggregation. Enumerating the values is a bit tricky in MySQL (unless you are using MySQL 8+):
select datetime,
max(case when rn = 1 then title end) as title_1,
max(case when rn = 1 then feedback end) as feedback_1,
max(case when rn = 2 then title end) as title_2,
max(case when rn = 2 then feedback end) as feedback_2,
max(case when rn = 3 then title end) as title_3,
max(case when rn = 3 then feedback end) as feedback_3,
max(case when rn = 4 then title end) as title_4,
max(case when rn = 4 then feedback end) as feedback_4,
max(case when rn = 5 then title end) as title_5,
max(case when rn = 5 then feedback end) as feedback_5,
max(case when rn = 6 then title end) as title_6,
max(case when rn = 6 then feedback end) as feedback_6
from (select gc.*,
(#rn := if(#dt = g.dateTime, #rn + 1,
if(#dt := g.dateTime, 1, 1)
)
) as rn
from (select g.dateTime, g.feedback, c.title AS title
from gauge g inner join
category c
on g.category_id = c.category_id and
c.title = 'title' and
g.feedback = 'feedback'
group by g.dateTime
order by g.dateTime
) gc cross join
(select #dt := '', #rn := 0) params
) gc
group by datetime;
I don't think the filtering on c.title and g.feedback is correct.
Not an optimized approach but you may join them into table and filter according to row number modulo by 6. This way, we can separate the 6 rows into 6 different table
http://rextester.com/DLVV64095
SELECT MAIN.DateTime,
TBL1.TITLE AS Title1,
TBL1.FEEDBACK AS Feedback1,
TBL2.TITLE AS Title2,
TBL2.FEEDBACK AS Feedback2,
TBL3.TITLE AS Title3,
TBL3.FEEDBACK AS Feedback3,
TBL4.TITLE AS Title4,
TBL4.FEEDBACK AS Feedback4,
TBL5.TITLE AS Title5,
TBL5.FEEDBACK AS Feedback5,
TBL6.TITLE AS Title6,
TBL6.FEEDBACK AS Feedback6
FROM (
SELECT [DATETIME] AS [DateTime]
FROM GAUGE
GROUP BY [DATETIME]
) MAIN
INNER JOIN (
SELECT G.DATETIME,
G.FEEDBACK,
C.TITLE,
ROW_NUMBER() OVER(PARTITION BY ORDER BY (SELECT 1) ASC) AS ROWNO
FROM GAUGE G
INNER JOIN CATEGORY C
ON G.CATEGORY_ID = C.CATEGORY_ID
) TBL1
ON TBL1.DATETIME = MAIN.[DateTime]
AND TBL1.ROWNO % 6 = 1
INNER JOIN (
SELECT G.DATETIME,
G.FEEDBACK,
C.TITLE,
ROW_NUMBER() OVER(PARTITION BY ORDER BY (SELECT 1) ASC) AS ROWNO
FROM GAUGE G
INNER JOIN CATEGORY C
ON G.CATEGORY_ID = C.CATEGORY_ID
) TBL2
ON TBL2.DATETIME = MAIN.[DateTime]
AND TBL2.ROWNO % 6 = 2
INNER JOIN (
SELECT G.DATETIME,
G.FEEDBACK,
C.TITLE,
ROW_NUMBER() OVER(PARTITION BY ORDER BY (SELECT 1) ASC) AS ROWNO
FROM GAUGE G
INNER JOIN CATEGORY C
ON G.CATEGORY_ID = C.CATEGORY_ID
) TBL3
ON TBL3.DATETIME = MAIN.[DateTime]
AND TBL3.ROWNO % 6 = 3
INNER JOIN (
SELECT G.DATETIME,
G.FEEDBACK,
C.TITLE,
ROW_NUMBER() OVER(PARTITION BY ORDER BY (SELECT 1) ASC) AS ROWNO
FROM GAUGE G
INNER JOIN CATEGORY C
ON G.CATEGORY_ID = C.CATEGORY_ID
) TBL4
ON TBL4.DATETIME = MAIN.[DateTime]
AND TBL4.ROWNO % 6 = 4
INNER JOIN (
SELECT G.DATETIME,
G.FEEDBACK,
C.TITLE,
ROW_NUMBER() OVER(PARTITION BY ORDER BY (SELECT 1) ASC) AS ROWNO
FROM GAUGE G
INNER JOIN CATEGORY C
ON G.CATEGORY_ID = C.CATEGORY_ID
) TBL5
ON TBL5.DATETIME = MAIN.[DateTime]
AND TBL5.ROWNO % 6 = 5
INNER JOIN (
SELECT G.DATETIME,
G.FEEDBACK,
C.TITLE,
ROW_NUMBER() OVER(PARTITION BY ORDER BY (SELECT 1) ASC) AS ROWNO
FROM GAUGE G
INNER JOIN CATEGORY C
ON G.CATEGORY_ID = C.CATEGORY_ID
) TBL6
ON TBL6.DATETIME = MAIN.[DateTime]
AND TBL6.ROWNO % 6 = 0
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
The result I receive:
id sku name GROUP_CONCAT(quantity_received) GROUP_CONCAT(item_cost)
4 00004 Antibacterial Wipes 50,14,25,309,50,14,25,309,50,14,25,309,50,14,25,309,50,14,25,309,50,14,25,309,50,14,25,309,50,14,25,309,50,14,25,309 3.29,3.29,3.29,3.49,3.29,3.29,3.29,3.49,3.29,3.29,3.29,3.49,3.29,3.29,3.29,3.49,3.29,3.29,3.29,3.49,3.29,3.29,3.29,3.49,3.29,3.29,3.29,3.49,3.29,3.29,3.29,3.49,3.29,3.29,3.29,3.49
The result I want:
id sku name GROUP_CONCAT(DISTINCT quantity_received) GROUP_CONCAT(DISTINCT item_cost)
4 00004 Antibacterial Wipes 50,14,25,309 3.29,3.49
The way I resolved this issue was placing DISTINCT in the quantity_recieved select. The problem is that if the quantity has two values that are the same such as 50, 50, 14, 25. The result will be 50, 14, 25. I just want to get rid of the repeating numbers and only get the values once.
Here is the query:
SELECT `product`.`id`,`product`.`sku`,`product`.`name`,
case when coalesce(stock1.`quantity`, '') = ''
then '0'
else stock1.`quantity`
end as qty_warehouse,
case when coalesce(sum(distinct stock2.`quantity`), '') = ''
then '0'
else sum(distinct stock2.`quantity`)
end as qty_events,
case when coalesce(stock1.`quantity`, '') = ''
then '0'
else stock1.`quantity`
end +
case when coalesce(sum(distinct stock2.`quantity`), '') = ''
then '0'
else sum(distinct stock2.`quantity`)
end as qty_total
GROUP_CONCAT(DISTINCT quantity_received) ,
GROUP_CONCAT(DISTINCT item_cost)
FROM (`product`)
LEFT JOIN`shipping_event`
ON `shipping_event`.`product_id` = `product`.`id`
LEFT JOIN `product_stock` as stock1
ON `product`.`id` = `stock1`.`product_id` and `stock1`.`location_id` = 112
LEFT JOIN `product_stock` as stock2
ON `product`.`id` = `stock2`.`product_id` and `stock2`.`location_id` != 112
LEFT JOIN `shipping_list`
ON `shipping_event`.`shipping_list_id` = `shipping_list`.`id`
WHERE `shipping_list`.`type` = 'incoming'
AND `shipping_event`.`end_date` > '2004-01-01 01:01:01'
GROUP BY `product`.`id`
ORDER BY `sku` asc LIMIT 20
I am using group concat just to display the result in this case. I actually sum the quantity_received and then multiple them by the item cost.
You could make a subquery with a GROUP BY and then JOIN to the product table.
The same logic can be applied to the other joined tables as well. If you do that, you can skip the GROUP BY product.id:
SELECT p.id
, p.sku
, p.name
, COALESCE(stock1.quantity, 0) --- minor improvements on
AS qty_warehouse --- the long CASE clauses
, COALESCE(SUM(DISTINCT stock2.quantity), 0)
AS qty_events
, COALESCE(stock1.quantity, 0) + COALESCE(SUM(DISTINCT stock2.quantity), 0)
AS qty_total
, grp.all_quantities_received
, grp.all_item_costs
FROM product AS p
LEFT JOIN
( SELECT product_id
, GROUP_CONCAT(se.quantity_received) AS all_quantities_received
, GROUP_CONCAT(se.item_cost) AS all_item_costs
FROM shipping_event AS se
LEFT JOIN shipping_list AS sl
ON se.shipping_list_id = sl.id
WHERE sl.type = 'incoming'
AND se.end_date > '2004-01-01 01:01:01'
GROUP BY se.product_id
) AS grp
ON grp.product_id = p.id
LEFT JOIN `product_stock` AS stock1
ON p.id = stock1.product_id
AND stock1.location_id = 112
LEFT JOIN product_stock AS stock2
ON p.id = stock2.product_id
AND stock2.location_id <> 112
GROUP BY p.id
ORDER BY sku ASC
LIMIT 20
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