Jump to content

Make an optional clause?


Garethp

Recommended Posts

I want to make a search where I have multiple WHERE clauses that are optional. I tried OR, but it doesn't give me what I want. Here's what I have

 

"SELECT members.ID, members.Username, COUNT(series.ID) as Stories FROM members, series WHERE members.Username LIKE '$Input%' AND (series.Author=members.ID OR series.CoAuthor LIKE concat('%;' + members.ID + ';%')) LIMIT $Start, $Limit"

 

The problem is that this excludes members who haven't Authored a series. I want to search ALL members, even if they haven't included a series, but also to fetch the series that they HAVE Authored, IF they have. I want to make it one SQL query so that I can ORDER BY COUNT(series.ID) or by another list of columns which I plan to add later

 

When I tried changing the AND to OR, it gave me the username of anyone who had written a story (I think), but what I want is to fetch series, if and only if they have Authored a series

 

How can I do this?

Link to comment
Share on other sites

I was thinking of doing something like this, would it work?

 

SELECT DISTINCT(members.ID) as ID, members.Username as Username
FROM members, series 
WHERE members.Username LIKE '$Input%' 
IF COUNT((series.Author=members.ID OR series.CoAuthor LIKE CONCAT ('%;', members.ID, ';%')) THEN AND (series.Author=members.ID OR series.CoAuthor LIKE CONCAT('%;',members.ID,';%')) 
LIMIT 0, 15

 

Would that work, and if so, how would I do it?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.