Handle race condition

Rewrite some core queries to handle race conditions more effectively

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/zencart/zc-v1-series/244)
<!-- Reviewable:end -->

Note: the extra backticks around `value` are simply to provide specificity since `value` is a MySQL keyword too.