我正在使用mysql 5.5,这是一个带有literal列表的左连接查询 :
select tbl1.*, details.*
from ('a', 'b', 'c'... 300+ elements) as 'tbl1'
left join details
on
details.id=tbl1.id
Run Code Online (Sandbox Code Playgroud)
但它不起作用!
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
如何把它list作为一个表?
使用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
Run Code Online (Sandbox Code Playgroud)
请参阅此Fiidle Logic
而不是使用子查询,你可以先创建一个表tbl1作为
create table tbl1
(
id varchar(1)
)
insert into tbl1
select 'a' as id
union
select 'b' as id
union
select 'c' as id
....300
Run Code Online (Sandbox Code Playgroud)
现在你可以使用表tbl1了join
select tbl1.*, details.*
from 'tbl1'
left join details
on
details.id=tbl1.id
Run Code Online (Sandbox Code Playgroud)