RIGHT JOIN返回所有NULL结果

时间:2015-12-18 19:51:39

标签: mysql

我有两张桌子: 我正在尝试从表1中检索字段namequantitycustom_message,并从表2加入正确的关联字段value - 给我:

name | quantity | custom_message | value

这是我的疑问:

SELECT vxu_4_wpsc_cart_contents.name, vxu_4_wpsc_cart_contents.quantity,
       vxu_4_wpsc_cart_contents.custom_message
FROM vxu_4_wpsc_cart_contents
RIGHT JOIN vxu_4_wpsc_submited_form_data 
  ON vxu_4_wpsc_cart_contents.id = vxu_4_wpsc_submited_form_data.id
WHERE form_id =2
OR form_id =3

正在返回

 name | quantity | custom_message
 NULL          NULL       NULL
 NULL          NULL       NULL + 3 more rows of nulls

vxu_4_wpsc_cart_contents

table 1

vxu_4_wpsc_submited_form_data

table 2

我只是不知道我哪里出错了!

1 个答案:

答案 0 :(得分:0)

我认为正确的加入应该是内部联接。请尝试以下查询:

SELECT
    vcc.name,
    vcc.quantity,
    vcc.custom_message,
    vfd.value
FROM
   vxu_4_wpsc_cart_contents AS vcc
   INNER JOIN vxu_4_wpsc_submited_form_data as vfd
         ON vcc.purchaseid = vfd.log_id
WHERE
   vfd.form_id IN (2,3);

(fyi:你的第二张图片链接与第一张相同:))