1
name   type   shape
---------------------
name1  type1  shape1
name1  type2  shape1
name1  type3  shape2

after query i need that result:

name    shape1Types     shape2Types
-----------------------------------
name1   type1, type2    type3

i can only think as much:

select name, group_concat(type) as shape1Types, group_concat(type) as shape2Types 
from table 
where name = 'name1' 
  and shape1Types in (select type from table2 where shape = 'shape1') 
  and shape2Times in (select type from table3 where shape = 'shape2') 
group by name

but here it says shape1Types is unknown column

2 Answers 2

1

try like below by using case when

   select name, group_concat(case when shape='shape1' then type end) as shape1Types, group_concat(case when shape='shape2' then type end) as shape2Types 
    from table 
    group by name
Sign up to request clarification or add additional context in comments.

Comments

0

The alias names shape1Types & shape2Types are unknown in that WHERE clause.
Hence the error.

Instead of using IN you could JOIN to the unique types in those other 2 tables.

select 
 t.name, 
 group_concat(shape1.type) as shape1Types, 
 group_concat(shape2.type) as shape2Types 
from table1 t
left join (select type from table2 where shape = 'shape1' group by type) shape1 
  on shape1.type = t.type
left join (select type from table3 where shape = 'shape2' group by type) shape2 
  on shape2.type = t.type
where t.name = 'name1'
-- and (shape1.type is not null or shape2.type is not null)
group by t.name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.