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 6939555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:38:16+00:00 2026-05-27T12:38:16+00:00

I’ve got a query like: SELECT * FROM table WHERE id=1 AND (field1=1 OR

  • 0

I’ve got a query like:

SELECT * FROM table WHERE id=1 AND (field1=1 OR field2=1 OR field3=1 OR field4=1 OR field5=1 OR field6=1 OR field7=1 OR field8=1)

The actual query is longer and there are more than 25 fields in the brackets…

How would I make sure that at least two of the fields = 1? If those were rows it would be easy, but what about columns? Not sure if it’s even possible.

Basically I need to run a php function only if there are at least two matching fields. The function then updates the selected fields so that they will not be selected again next time.

  • 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-27T12:38:16+00:00Added an answer on May 27, 2026 at 12:38 pm

    In MySQL, conditional statements can be evaluated directly, and they return “1” if they are true and “0” if they are false. So in this case, you can just sum the conditionals like so:

    SELECT * FROM table WHERE id=1 AND (field1=1) + (field2=1) + (field3=1) + (...) >= 2
    

    If the sum is equal to or greater than 2 then at least two fields have the value “1”. Note that you must put parenthesis around the conditionals here to make sure they are individually evaluated; otherwise it doesn’t parse correctly.

    In the general case, there is also the MySQL’s IF function. You could use it here like this:

    SELECT * FROM table WHERE id=1 AND IF(field1=1, 1, 0) + IF(field2=1, 1, 0) + IF(field3=1, 1, 0) + (...) >= 2
    

    IF() takes three arguments. The first is a condition. The second is the return value if the condition is true. The third is the return value if the condition is false. So this would work too since in MySQl, the value of a bare conditional is equivalent to the value of IF(conditional, 1, 0).

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
i got an object with contents of html markup in it, for example: string

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.