2

我正在使用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做成桌子??

4

3 回答 3

6

使用联合

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

请参阅此文件逻辑

您可以先创建一个表,而不是使用Subquerytbl1

create table tbl1
(
id varchar(1)
)

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

现在您可以使用表tbl1join

select tbl1.*, details.* 
from 'tbl1' 
left join details
on 
details.id=tbl1.id
于 2013-04-26T03:40:55.247 回答
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;
于 2013-04-26T03:25:27.577 回答
-1
select tbl1.*, details.* 
from (select 'a', 'b', 'c' as id) as tbl1
left join details
on 
details.id=tbl1.id
于 2013-04-26T03:28:44.970 回答