Is a crossover query equivalent to a where a and b?

I have a DB with postgresql and I wrote two simple queries

select page from title where word = 'france' and part = 'headline'; 

and

 select page from title where word = 'france' intersect select page from title where part = 'headline'; 

I think that they should return the same result, but actually it is different. Any suggestions?

The table structure is just id, word, page, part.

EDIT:

I also tried

select separate

but a query with intersect always returns some irrelevant results. This is a DB of a simple reverse table of some news web pages. Thus, the page, word, and part are not unique. But there are no duplicate entries.

+4
source share
2 answers

WHERE (a) AND (b) is the Boolean condition applied to each record. A record is included only if it satisfies all conditions. In other words, only entries in which the word is 'france' AND part are the β€œheading” will be included at the same time.


What you need is more like using OR in your state?

 select page from title where word = 'france' or part = 'headline'; 


Or are you having problems with multiple posts that link to the same page?

For instance...

 1 | 'france' | 'aaa' | 'headline' 2 | 'france' | 'bbb' | 'body' 3 | 'germany' | 'bbb' | 'headline' 

'aaa' will be returned by your requests.

'bbb' will only be returned by your second request.

+4
source

The only technical difference that I see is that the crossing operation would create a unique page s. The first can generate duplicates.

Update: They are not the same. the correct answer is what Dems has already explained (I voted for his answer):

 page word part 1 france headline 2 uk headline 2 france body 

word = france AND part = headline => page 1

word = france => page 1, 2 part = headline => page 1, 2

intersection of the previous two sets => p. 1, 2

Update2: The answer to the question: how to make intersect give the same result? The intersection should be performed on the column under conditions like here.

 select page, word, part from title where word = 'france' intersect select page, word, part from title where part = 'headline' 
+4
source

Source: https://habr.com/ru/post/1388816/


All Articles