Question:

Is it possible to use a wild card selector in MySQL "WHERE" queries

by  |  earlier

0 LIKES UnLike

I want to build a query that allows users to choose between selecting a subset of records or choosing ALL records.

"SELECT * FROM items WHERE authorized = 'Yes' AND order = 1"

Creates the subset.

Without building an entirely new query, how can I alter this to select all records? For example I would like to write:

"SELECT * FROM items WHERE authorized = * AND for_sale = * "

But this is invalid SQL syntax.

Is there a valid way to do it?

As I said, I DONT want to have to write multiple query lines.

 Tags:

   Report

3 ANSWERS


  1. Just drop the where clause and have SELECT * FROM items

    That will return all rows.

    EDIT: If you only want one, then construct it on the fly using whatever programming language you are surrounding your query with.


  2. I believe if your using MySQL Query Analyzer Browser you can paste in a query.   If you highlight the SELECT statement and FROM clause only that will run.  If you highlight the first three lines only that will run.  If you highlight all of them or run them without being highlight all four lines will run.  If you are using a command line prompt then I say you need multiple queries.

    SELECT *

    FROM Customers

    WHERE Country LIKE 'USA'

    AND (ContactTitle LIKE 'Owner' OR Region LIKE 'CA')

  3. I think, but I'm not entirely certain, that what you need is this:

    SELECT * FROM items WHERE authorized like '%'  AND order like '%'

    and when you want to just choose the subset

    SELECT * FROM items WHERE authorized like 'Yes'  AND order like '1'

    and you programatically replace the 'Yes' and the '1' with the % sign in those cases.  (You are passing in with some variable that 'yes' and '1'?)  Or perhaps I don't understand the question -- how does the user decide whether to look at all records or just a subset?  

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.