Added GROUP on query to avoid dupes.
Fixing queries to work on MySQL for IIS by making sure to pass intval, etc.