简体   繁体   中英

How to select only those comments where associated posts's privacy is 0

I have a table "Posts" that looks like this-

------------------------------------
id  |  Title | Date | Privacy 
1   | abc    | 2016 | 0
2   | xyz    | 2015 | 1

And another table "comments" that looks like this-

------------------------------------
id  | post_id | content | Date   
1   |  2       |  abc    | 2016 
2   |  1       |  xyz    | 2015 

I need to select all the comments -

a) whose post's privacy is 0
b) Where comments.content is like '%keyword%'
c) order by comments.date
d) Limit and offset values

Your help will be much appreciated. Thanks!

Do you mean:

SELECT content 
FROM comments C
INNER JOIN Posts P ON P.id = C.post_id
WHERE content like '%keyword%' 
AND Privacy = 0
ORDER BY C.Date

Please explain what you mean with: " d) Limit and offset values "

You should first search for an answer already given on stack overflow or just make a google search.

Anyway here is your answer

select * from comments where 
content like '%keyword%' and
post_id in ( select id from Posts where Privacy = 0)
order by Date
LIMIT 10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM