views:

50

answers:

1

Hi guys, RoR/SQL newbie here.

My datetime column 'deleted_at' are all uninitialized. Running this query returns an error:

SELECT * FROM variants v
 ON v.id = ovv0.variant_id INNER JOIN option_values_variants ovv1
 ON v.id = ovv1.variant_id INNER JOIN option_values_variants ovv2
 ON v.id = ovv2.variant_id INNER JOIN option_values_variants ovv3
 ON v.id = ovv3.variant_id INNER JOIN option_values_variants ovv4
 ON v.id = ovv4.variant_id
 WHERE v.deleted_at = NULL
 AND v.product_id = 1060500595

However, if I set my datetime values to 0, and set my query to v.deleted_at = 0, the correct variant is returned to me.

How do I check for uninitialized/NULL datetimes?

A: 

Your main error I believe is that ovv0 is not defined. State your inner join, then your clause.

SELECT * FROM variants v 
INNER JOIN option_values_variants ovv0 ON v.id = ovv0.variant_id
INNER JOIN option_values_variants ovv1 ON v.id = ovv1.variant_id
INNER JOIN option_values_variants ovv2 ON v.id = ovv2.variant_id
INNER JOIN option_values_variants ovv3 ON v.id = ovv3.variant_id
INNER JOIN option_values_variants ovv4 ON v.id = ovv4.variant_id
WHERE v.deleted_at = NULL AND v.product_id = 106050059;

I'd also like us to explore why you are using so many inner joins on the same table. What are you trying to accomplish?

EDIT:

For nulls, SQLite is tricky, and considers null to be a type. There is also no true datetime type either. You can verify the column type with the typeof function, which returns a string.

SELECT * FROM variants v 
...
WHERE typeof(v.deleted_at) = 'null' AND v.product_id = 106050059;

REEDIT:

There's also ifnull. That function replaces a null with whatever value you would like. So in your case, with the single command you can check natural 0's and nulls in one shot. If you'd rather not have natural 0's and just the nulls, use some other value that should not be a date (say -1) and evaluate against it.

SELECT * FROM variants v 
...
WHERE ifnull(v.deleted_at,0) = 0 AND v.product_id = 106050059;
MPelletier
Hi MPelletier, v.deleted_at = NULL is the problem as my temporary solution to do v.deleted_at = 0 runs fine.I meant to say it returns nil.Its a product (shirt) with many variants, which are defined by their options(red, blue, S/M/L).My main question is still: How do I check for a null value from a datetime field in sqlite?
Paul N
@Paul N, I did a few checks, see edited response.
MPelletier
@MPelletier, checking for the type was a pretty creative solution. I got it working with my development db =]. Sorry I don't have upvote privileges yet.Fixing this marks the end of my web dev internshp! Thanks so much.
Paul N
@Paul N. I thought about it some more and I think I'd prefer `ifnull` over checking the type. Also, I had double equal signs, not required in SQL. And last, if you wish, you can click the check mark to accept the answer. It will give you a badge for first accepted answer and some reputation (2 pts, I think).
MPelletier
@Paul N And congratulations on ending your internship!
MPelletier
@MPelletier. I did have to tweak the query a bit. Sqlite didn't exactly like the double = or ". Thanks for taking some time to investigate. Hopefully this will be useful to someone later on.
Paul N