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

  • Home
  • SEARCH
  • 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 274629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:38:21+00:00 2026-05-12T00:38:21+00:00

I have written a query to find similar ids based on tags from a

  • 0

I have written a query to find similar ids based on tags from a current id, much like amazons, you would also like. The problem is, in my example I want to exclude bookid 30 from this search.

Here is my query:

note: $similar is basically a string filled with tags built up with a few “like %item% or”

$query = "SELECT * FROM books
          WHERE bookid !=30
          AND {$similar}
          AND visible ='1'
          AND level ='2'
          LIMIT 3";

The part that is not working is where bookid!= I’ve tried IS NOT, <> reordering and what ever else I could find. This query still works it outputs three rows but still includes bookid 30

Can someone explain what is going here? is my query becoming too intricate and need to be restructured?

The poor alternative i’ve came up with was making the limit 4 items sending it to a while loop to fetch each row and exclude the bookid i don’t need. This just seems pointless when I know it can be done in the query.

Ive fixed this by rebuilding my query and $similar string
Before

SELECT * FROM books WHERE bookid !=30 AND tags LIKE '%one%' OR tags LIKE '% two%' OR tags LIKE '% three%' AND visible ='1' AND level ='2' LIMIT 3

After

SELECT * FROM books WHERE bookid !=30 AND (bookid!=30 AND tags LIKE '%one%') OR (bookid!=30 AND tags LIKE '% two%') OR (bookid!=30 AND tags LIKE '% three%') AND visible ='1' AND level ='2' LIMIT 3

One thing I did notice which is strange is bookid!= only works when there is one at the beginging and between each like or… i tried swaping each out and the query would not run, but current string works perfectly!

Thanks,

  • 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-12T00:38:22+00:00Added an answer on May 12, 2026 at 12:38 am

    Make sure that your $similar variables are wrapped in parentheses.

    if an OR is in the middle, it will be evaluated as

    (bookid != 30 AND item like '%item1%') OR (item like '%item2%' AND visible = 1 AND level= 2)
    

    instead of

    bookid != 30 AND (item like '%item1%' OR item like '%item2%') AND visible = 1 AND level= 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written this query for retrieving data from mysql as below select FeedbackCode,EMailID,FeedbackDetail,
I have written a query in Sql server 2008. select select * from program
I have written a query like this. It is working and giving me the
I have written a PHP script to pull a query from an Oracle database
I have a query written in Mongoid @result = User.find(:name=>xxxxx).limit(5) and I return the
i have written insert query for my application to create new user with password,
I have written media query for 3 different dimensions. Intial style is for 320px
I have written the following query, but in the WHERE clause I have used
I have written a DB2 query to do the following: Create a temp table
I have written a SQL query that works just fine, but am having a

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.