Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to query from a literal list : select * from ('a', 'b', 'c')

Tags:

sql

mysql

i'm using mysql 5.5, here is a left join query with a literal list:

select tbl1.*, details.* 
from ('a', 'b', 'c'... 300+ elements) as 'tbl1' 
left join details
on 
details.id=tbl1.id

but it doesn't work!

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''a','b')' at line 1

how to make this list as a table??

like image 649
Scott 混合理论 Avatar asked Oct 23 '25 04:10

Scott 混合理论


1 Answers

Use UNION

select tbl1.*, details.* 
from (select 'a'as id
      union
      select 'b' as id
      union
      select 'c' as id
      union
      ...300) as tbl1
left join details
on 
details.id=tbl1.id

See this Fiidle Logic

Instead of using Subquery, you can first create a table tbl1 as

create table tbl1
(
id varchar(1)
)

insert into tbl1
select 'a' as id
union 
select 'b' as id
union 
select 'c' as id
....300

now you can use table tbl1 for join

select tbl1.*, details.* 
from 'tbl1' 
left join details
on 
details.id=tbl1.id
like image 162
Prahalad Gaggar Avatar answered Oct 24 '25 19:10

Prahalad Gaggar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!