How to select entries from this relationship?

后端 未结 2 1517
你的背包
你的背包 2021-01-26 05:59

I have these four tables: feeds, feed_entries, entries_categorias and categorias. Whith these structures:

CREATE TABLE `categorias` (
  `id` int(11) NOT NULL aut         


        
相关标签:
2条回答
  • 2021-01-26 06:36

    Try something like this:

    SELECT * 
    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 c.nome IN ('Google','Apple') 
        AND (e.deleted =0)
        GROUP BY e.id
        HAVING COUNT(DISTINCT ec.id) = 2
    ) 
    
    0 讨论(0)
  • 2021-01-26 06:38

    Change your WHERE clause to use an IN operator:

    SELECT `e`.* 
    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 (c.nome IN (<List of categories separated by commas>) AND (e.deleted =0) 
    ORDER BY `e`.`date` DESC
    
    0 讨论(0)
提交回复
热议问题