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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:44:38+00:00 2026-05-25T19:44:38+00:00

I am writing a query that will retrieve data that match a set of

  • 0

I am writing a query that will retrieve data that match a set of criteria. Though the number and value of these criteria is unknown. It could be “name = username” and/or “date = today” and/or “title= hello” and so on.

There are almost 10 different criteria and there could be only one selected as there could be 10 of them, so writing a query for each scenario is not likely.

Does MySql have a syntax for such scenario ? If not what would be the best way to achieve that result ?

  • 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-25T19:44:38+00:00Added an answer on May 25, 2026 at 7:44 pm

    Columns and expressions in the WHERE clause must be fixed at the time you prepare the query. There’s no way to parameterize columns or expressions in SQL.

    The way most people solve this problem is by writing application code to add terms to your WHERE clause based on application logic and variables. In other words, append fragments of SQL syntax to a string inside code constructs like if (). The specific methods for doing this depend on the programming language you’re using.

    To help in this technique, sometimes frameworks provide a query builder API. For example, I worked on the Zend Framework, which includes a class called Zend_Db_Select. It helps you append terms to a query. The result is simply a string, which you can then prepare as an SQL statement.


    I don’t work on Drupal, but I see in their online manual they have an API method for adding terms to an SQL WHERE clause, similar to what I was describing above.

    I suppose that if you had a PHP associative array of key/value pairs such that the key is the column name and the value is the value to search for, you could do this:

    <?php
    
    $query = ...  // create the query object
    
    $search_conditions = array(
      name => "username",
      date => "today",
      title => "hello"
    );
    
    foreach ($search_conditions as $col => $val) {
      $query = $query->condition($col, $val, "=");
    }
    
    $query->execute();
    

    The manuals says:

    A conditional object consists of one or more conditional fragments that will all be joined by a specified conjunction. By default, that conjunction is AND.

    So you can add zero, one, or more conditions to the query, and it keeps track of them, as if you had written the following:

    WHERE name = :name AND date = :date AND title = :title
    

    When it is time to execute the query, it automatically passes the values you gave (“username”, “today”, “hello”) for those parameter placeholders.

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

Sidebar

Related Questions

I need help writing a T-SQL query that will generate 52 rows of data
Given a username, how would I go about writing an LDAP query that will
I am writing a program that will query an MS access database, return the
I'm writing a python CGI script that will query a MySQL database. I'm using
I am writing an SQL query in which that I will need to perform
I am writing a shared library that will allow linked applications to query a
I've got a query which returns 30 rows. I'm writing code that will paginate
I'm writing a view that will have daily/weekly/monthly report data. I'm thinking it makes
I am writing a query that will be used in a .NET application, therefore
Whats the best approach for writing data to a file that will be picked

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.