如何从文字列表中查询:select * from('a','b','c')

时间:2013-04-26 03:21:35

标签: mysql sql

我正在使用mysql 5.5,这是一个带有literal列表的左连接查询:

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

但它不起作用!

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作为表格?

3 个答案:

答案 0 :(得分:6)

使用 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

请参阅 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

现在您可以将表格tbl1 用于join

select tbl1.*, details.* 
from 'tbl1' 
left join details
on 
details.id=tbl1.id

答案 1 :(得分:0)

在您的子查询中缺少一个选择?

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

但我不明白你的左连接应该做什么

[编辑]为您的加入创建临时表?

CREATE TEMPORARY TABLE TempTable ( tempcol varchar(100) ) ENGINE=heap; 

insert into TempTable
values ('a'), ('b'), ('c'), 300+ elements);

select TempTable.*, details.*
from TempTable
left join details on details.id = TempTable.tempcol;

答案 2 :(得分:-1)

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