user1759136
user1759136

Reputation:

Querying MySQL database

I am trying to query the MySQL database to return some results. For e.g. I have a HTML form where user can enter some search values and can execute it. This form contains two fields: Body and Query String (these are appropriate fields with my database table).

I am executing the query like this:

select count(*) from Message;

The result is: 7280

If I execute the query like this:

select count(*) from Message where body like '%%';

The result is: 7280

The body does not have any null values.

But if I add another empty like into the query:

select count(*) from Message where body like '%%' and queryString like '%%';

There result is: 3353.

My queryString column contains some null values, which I am guessing is causing this. What I would like that query to return is the same count of results (7280) and ignore those null values. How to achieve this using MySQL?

Upvotes: 0

Views: 64

Answers (2)

Subodh
Subodh

Reputation: 2214

You can make use of IFNULL function :

select count(*) from Message where body like '%%' and IFNULL(queryString, " ") like '%%';

Upvotes: 1

DavidB
DavidB

Reputation: 2596

WHERE ((body LIKE '%%') OR (body IS NULL))
and   ((queryString LIKE '%%') OR (queryString IS NULL))

Upvotes: 1

Related Questions