想要使用另一个表的列值的“in”语句来提取所有记录

时间:2014-01-29 04:08:40

标签: mysql

我有以下表结构:

所有者

OwnerId - PKey  
ActionListId - FKey

的ActionList

ActionListId - PKey  
Actions - VARCHAR(8000)

动作

ActionId - PKey

ActionList.Actions的格式如下:

ActionId1, ActionId2, ActionId3, ActionId4, ... ActionIdN

我正在尝试执行此查询:

select 
    o.*,
    a.Name
from Owner as o
join ActionList as al on al.ActionListId = o.ActionListId
join Action as a on a.ActionId in (al.Actions)
where o.OwnerId = 1;

但是我收到了一条记录。为什么第二个连接没有成功加入整个数组而只是第一个元素?有没有办法通过一个查询获取ActionList中的所有动作?

1 个答案:

答案 0 :(得分:0)

试试这个

SELECT O.*,A.Name
FROM Owner AS O
     LEFT JOIN ActionList AS AL ON 
               AL.ActionListId = O.ActionListId
     LEFT JOIN Action AS A ON 
               A.ACtionID IN (SELECT ACTIONS FROM ACTIONList)
WGERE O.OwnerId = 1;