开发者

How can I add other columns from a foreign table in the sql?

开发者 https://www.devze.com 2023-01-09 07:30 出处:网络
See the tables structure here. I have this sql: SELECT `feed_entries` . * FROM `feed_entries` WHERE id IN (

See the tables structure here.

I have this sql:

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

SELECT `e`.`id`
FROM `feed_en开发者_JAVA技巧tries` 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 

And I want to add these fields:

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

How can I do that? Without get this error:

#1241 - Operand should contain 1 column(s) 


This should work:

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

I removed most of the back-ticks because they look very peculiar to me - but I don't use MySQL.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号