Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

MySQL Optional WHERE clause

Name: Anonymous 2012-09-26 7:51

So I'm doing some mostly menial programming task for some random company.

I've stumbled upon something though, that I cannot seem to figure out.

For MySQL, if I have this long select statement where all of the WHERE clause is optional, how do I handle that for integers? It's easy enough for Strings as you simply make a 0 char string when you initialize the variable, however for integers, you have to initialize them with something, which in this case is 0.

What this does is that when I have my SELECT statement, I can't just put in '%0%' as it would search for everything containing that 0. How do I make it think that the 0 is actually a null so it does not include it in the WHERE clause and is essentially ignored?

Not sure how else to explain the question.

Name: Anonymous 2012-09-26 7:55

Terrible!

Name: Anonymous 2012-09-26 7:59

It should be said that I'm using the J/Connector to access the database through Java, which is why I have to initialize the variables to something, in this case 0.

Name: Anonymous 2012-09-26 9:07

Use a magic number that you know will never be a value for that field.

Name: Anonymous 2012-09-26 9:17

You need to get a better DAL wrapper. Here's what I have in mine:

params = {id: 42};
rows = db.query('select * from foobar [where id = :id]', params);

The part where it says '[where id = :id]' is optional and will only be included if params contains all the needed placeholders, in this case id. You can even nest the optional query fragments.

Too bad I'm not going to open source my god-tier library, OP.

Name: Anonymous 2012-09-26 9:57

DBMSs are shit.

Name: Anonymous 2012-09-26 10:50

>>1
Hello,

Do us a favor and stop programming.

Please.

Name: Anonymous 2012-09-26 15:10

where (@var is null or column = @val)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List