2

I need to select all rows from table: Announcements where there are exist rows in table Categories_announcements for user by condition:

Categories_user.category_id = Categories_announcements.category_id

I tried SQL query, see link

It should returns me onlu one row Announcements.id = 1, because user has only one category in Categories_user.category_id.

EDIT:

I have tested SQL that you shared, so this is query:

select *
from `announcements`
where exists (
        select 1
        from `announcement_category`
        inner join `user_category` on `user_category`.`category_id` = `announcement_category`.`category_id`
        where `Auser_category`.`user_id` = 1
            and `announcement_category`.`announcement_id` = announcements.id
        )

It returns me an error:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') LIMIT 0, 25' at line 7

1
  • 3
    Using SQLFiddle is fine. But you should also add the query to you question. Commented Feb 25, 2017 at 15:56

5 Answers 5

2

I don't think you need an EXISTS subquery. A basic join statement should work.

select a.* 
from Categories_user cu
join Categories_announcements ca on ca.category_id = cu.category_id
join Announcements a on a.id = ca.announcement_id
where cu.user_id = 1
Sign up to request clarification or add additional context in comments.

2 Comments

Can you help me write this query in Laravel eloquent?
@Darama If you want an eloquent solution you would need to define the models and relations first. But i wouldn't change this question too much, because you've already got a lot of answers. Write a new question tagged with laravel. After defining the relations the answer might be as simple as $user->announcements;.
1

You need to correlate the subquery with the Announcements table:

select *
from Announcements a
where exists (
        select 1
        from Categories_announcements ca
        inner join Categories_user cu on cu.category_id = ca.category_id
        where cu.user_id = 1
            and ca.announcement_id = a.id
        )

Demo

Comments

1

You are close. Try this:

select a.*
from Announcements a
where exists (select 1
              from Categories_announcements ca join
                   Categories_user cu
                   on cu.category_id = ca.category_id 
              where ca.announcement_id = a.id and
                    cu.user_id = 1
             );

Notes:

  • The key thing you are missing is the correlation clause to the outer query (ca.announcement_id = a.id).
  • The left join is superfluous. The where clause turns it into an inner join.
  • Table aliases make the query easier to write and to read.

Comments

1

You'r query is missing a relationship between the announcement table in the outer query to the condition in the inner one:

SELECT *
FROM   announcements a
WHERE  EXISTS (SELECT    * 
               FROM      categories_announcements ca
               LEFT JOIN categories_user cu ON cu.category_id = ca.category_id 
               WHERE     cu.user_id = 1 AND
                         a.id = ca.announcement_id -- Here!
              ) 

SQLFiddle

1 Comment

What about perfomance? is it speed request for 1000 announcements?
1

Another possible workaround without a sub-query:

SELECT a.id AS accouncement_id, a.name AS annoucement_name 
FROM Categories_user cu
INNER JOIN Categories_announcements ca 
        ON  cu.category_id = ca.category_id
        AND cu.user_id = 1
INNER JOIN Announcements a 
        ON ca.announcement_id = a.id;

Demo link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.