如何在sql中添加外表中的其他列?

时间:2010-07-27 21:50:11

标签: sql mysql

See the tables structure here.

我有这个sql:

SELECT `feed_entries` . *
FROM `feed_entries`
WHERE 
id
IN (

SELECT `e`.`id`
FROM `feed_entries` AS `e`
INNER JOIN `feeds` AS `f` ON e.feed_id = f.id
INNER JOIN `entries_categorias` AS `ec` ON ec.entry_id = e.id
INNER JOIN `categorias` AS `c` ON ec.categoria_id = c.id
WHERE 
  e.deleted =0
AND 
  c.slug
  IN ('manchete', 'google')
GROUP BY `e`.`id`
HAVING COUNT( DISTINCT ec.id ) =2

)

ORDER BY `date` DESC
LIMIT 1 

我想添加这些字段:

`f`.`titulo` AS `feedTitulo` , `f`.`url` AS `feedUrl`

我该怎么做?没有得到这个错误:

#1241 - Operand should contain 1 column(s) 

1 个答案:

答案 0 :(得分:1)

这应该有效:

SELECT e1.*, f1.titulo AS feedTitulo, f1.url AS feedUrl
  FROM feed_entries AS e1 JOIN feeds AS f1 ON e1.feed_id = f1.id
 WHERE f1.id IN
            (SELECT e.id
               FROM feed_entries            AS e
              INNER JOIN feeds              AS f  ON e.feed_id = f.id
              INNER JOIN entries_categorias AS ec ON ec.entry_id = e.id
              INNER JOIN categorias         AS c  ON ec.categoria_id = c.id
              WHERE e.deleted = 0
                AND c.slug IN ('manchete', 'google')
              GROUP BY e.id
             HAVING COUNT(DISTINCT ec.id) = 2
            )
 ORDER BY `date` DESC
 LIMIT 1

我删除了大部分的后退,因为它们看起来非常奇怪 - 但我不使用MySQL。