使用一个公共字段从列获取数据?

时间:2013-11-19 21:02:13

标签: php mysql

表exp_channel_data as ecd:

id | field_id_16| img_src

1  |  2        | a.jpg
2  |  1        | b.jpg
3  |  3        | c.jpg

表pin_info为pi:

id | look_week | member_id | is_pinned

1  |  2        | 2         | yes
2  |  1        | 2         | yes
3  |  3        | 1         | yes

我想做什么:

从表ecd中获取一个img src数组作为成员(比方说2)和is_pinned = yes。我唯一的常见字段是field_id_16和look_week。

我做了什么:

$sql="
  SELECT 
    ecd.field_id_17 as pin_img
  FROM exp_channel_data ecd
  WHERE 
     ecd.field_id_16=(
       SELECT look_week 
       FROM pin_info pi 
       WHERE
         pi.member_id='$member_id' 
         AND is_pinned='yes'
     )
";

我无法获取数组。错误是子查询返回超过1行

2 个答案:

答案 0 :(得分:1)

SELECT 
    ecd.field_id_17 as pin_img
  FROM exp_channel_data ecd
  WHERE 
     ecd.field_id_16 IN (
       SELECT look_week 
       FROM pin_info pi 
       WHERE
         pi.member_id='$member_id' 
         AND is_pinned='yes')

答案 1 :(得分:1)

这是正确的,因为你有两行成员身份2尝试加入

SELECT ecd.img_src as pin_img FROM exp_channel_data ecd 
JOIN pin_info pi ON (ecd.field_id_16 = pi.look_week )
WHEHER pi.member_id='$member_id' AND pi.is_pinned='yes'

我猜您field_id_17

中没有exp_channel_data