Using multiple AND's and OR's

Posted on 2012-08-15
Last Modified: 2012-08-15

I have the following query which basically, I want it to do the following:

select the field tblhostingaddons where the status is pending and the hostingid is 1
select one that has either the addonid 1 or the addonid 2.

The conditions that the status is pending and the hostingid is 1 must be there, but the condition for addonid can be either 1 or 2.

This is my query:

"SELECT id FROM tblhostingaddons WHERE status='Pending' AND hostingid='1' AND addonid=1 OR addonid =2"

However, it's not following my conditions that the status must be pending. It's choosing one where the status isn't pending which is wrong.

Any ideas where I'm going wrong here?
Question by:rvcw
    1 Comment
    LVL 82

    Accepted Solution

    Try grouping the addonid ...

    "SELECT id FROM tblhostingaddons WHERE status='Pending' AND hostingid='1' AND (addonid=1 OR addonid =2)"

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    PRTG Network Monitor: Intuitive Network Monitoring

    Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

    I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
    Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now