Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6631173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:30:32+00:00 2026-05-25T22:30:32+00:00

Here in the query when i am trying to execute the OR condition before

  • 0

Here in the query when i am trying to execute the OR condition before AND conditions it gives wrong result..as web_filename_username_4hr_ts_201109 also gets included..
Wrong Result Query:

SELECT tablename FROM pg_tables
 WHERE schemaname = 'public'
   and (tablename like 'web%_4hr_ts_%'
       and tablename not like 'webs%_4hr_ts_%'
       and tablename not like 'webr%_4hr_ts_%'
        or tablename like '%web%summary%_4hr_ts_%'
       and tablename not like 'web_filename_username%_4hr_ts_%'
       )
     ;

But when i executed all AND before ORing it gives Correct result…
Correct Result Query:

SELECT tablename
  FROM pg_tables
 WHERE schemaname = 'public'
   and (tablename like 'web%_4hr_ts_%'
    and tablename not like 'webs%_12hr_ts_%'
    and tablename not like 'webr%_4hr_ts_%'
    and tablename not like 'web_filename_username%_4hr_ts_%'
     or tablename like '%web%summary%_4hr_ts_%'
       )

I dont know why….?????????

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:30:33+00:00Added an answer on May 25, 2026 at 10:30 pm

    Because the way you have written it it checks in to blocks.

    tablename like 'web%_4hr_ts_%'
    and tablename not like 'webs%_4hr_ts_%'
    and tablename not like 'webr%_4hr_ts_%'

    or

    tablename like '%web%summary%_4hr_ts_%'
    and tablename not like 'web_filename_username%_4hr_ts_%'

    now obviously tablename can not match both '%web%summary%_4hr_ts_%' and 'web_filename_username%_4hr_ts_%' so thats always going to be false.

    Here is what explain says about it

     Nested Loop  (cost=0.00..28.55 rows=1 width=64)
       Join Filter: (c.relnamespace = n.oid)
       ->  Seq Scan on pg_class c  (cost=0.00..27.45 rows=1 width=72)
             Filter: ((relkind = 'r'::"char") AND (((relname ~~ 'web%_4hr_ts_%'::text) AND (relname !~~ 'webs%_4hr_ts_%'::text) AND (relname !~~ 'webr%_4hr_ts_%'::text)) OR ((relname ~~ '%web%summary%_4hr_ts_%'::text) AND (relname !~~ 'web_filename_username%_4hr_ts_%'::text))))
       ->  Seq Scan on pg_namespace n  (cost=0.00..1.09 rows=1 width=4)
             Filter: (n.nspname = 'public'::name)
    

    As you can see its added brackets showing that the OR is at the top level. This is why when Im using any OR with AND I always use brackets to ensure the correct evaluation order.

    If your ever wondering why a statement doesn’t give you what you expect just stick explain in front of it and see what its going to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's an example of the query I'm trying to convert to LINQ: SELECT *
Here is my query Select Gender from Table The result pane shows 1 1
Here is the query I'm trying to execute, and it's supposed to return a
I am trying to execute the following query SELECT a.userid, COUNT(a.id) AS count_answers, SUM(v.yes_vote)
I'm trying to write a linq to object query in vb.net, here is the
Here's the query: SELECT COUNT(*) AS c, MAX(`followers_count`) AS max_fc, MIN(`followers_count`) AS min_fc, MAX(`following_count`)
Here is the query that I am working on: SELECT `unitid`, `name` FROM apartmentunits
Here is my Query : select EmployeeName, EmployeeSalary from Employee2 for xml path('EmployeeDetails') returns
So firstly here's my query: ( NOTE:I know SELECT * is bad practice I
I am trying to execute a query on a table in my SQL Server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.