如何阅读本文中的多个专栏

时间:2012-11-20 16:46:03

标签: mysql

在1_products t2中,我有其他名为make,model

的列

当我添加它们时

(SELECT 
            t2.code,t2.make,t2.model
        FROM .....

我得到了

  

错误1241操作数应包含1列

SELECT 
    t1.fk_products_id,
    (SELECT 
            t2.code
        FROM
            1_products t2
        WHERE
            t2.id = t1.fk_products_id
        order by code
        limit 1)
FROM
    1_stock t1
WHERE
    t1.branch = 1 and t1.dispatch <> 0;

1 个答案:

答案 0 :(得分:2)

您不能在内联查询中返回多个列。如果要为每个t1.fk_products_id返回多个列,则必须重写与此类似的查询:

SELECT t1.fk_products_id,
  t2.code,
  t2.make,
  t2.model
FROM 1_stock t1
LEFT JOIN 1_products t2
  on t1.fk_products_id = t2.id
WHERE t1.branch = 1 
  and t1.dispatch <> 0;