Note that there are some explanatory texts on larger screens.

plurals
  1. POarrange sql statement according to meta value not working
    primarykey
    data
    text
    <p>i'm pulling my hair out here:</p> <p>I have a sql statement that is part of an ajax call:</p> <pre><code>SELECT $wpdb-&gt;commentmeta.meta_value, $wpdb-&gt;comments.comment_ID, $wpdb-&gt;comments.comment_date, $wpdb-&gt;comments.user_id, $wpdb-&gt;comments.comment_content, $wpdb-&gt;comments.comment_author FROM $wpdb-&gt;commentmeta JOIN $wpdb-&gt;comments ON $wpdb-&gt;comments.comment_ID = $wpdb-&gt;commentmeta.comment_id WHERE $wpdb-&gt;commentmeta.meta_key = '_commentsvote' AND $wpdb-&gt;comments.comment_date &gt;='" . $current_year . "-" . $current_month . "-01' AND $wpdb-&gt;comments.comment_date &lt;='" . $current_year . "-" . $current_month . "-31' ORDER BY $wpdb-&gt;commentmeta.meta_value DESC </code></pre> <p>the statement works great, and returns results, however, the order is off. now, i know this is because the meta_value is actually a varchar, and not an int. so, the internets told me to do either a convert or cast:</p> <pre><code>Convert: SELECT $wpdb-&gt;commentmeta.meta_value, $wpdb-&gt;comments.comment_ID, $wpdb-&gt;comments.comment_date, $wpdb-&gt;comments.user_id, $wpdb-&gt;comments.comment_content, $wpdb-&gt;comments.comment_author FROM $wpdb-&gt;commentmeta JOIN $wpdb-&gt;comments ON $wpdb-&gt;comments.comment_ID = $wpdb-&gt;commentmeta.comment_id WHERE $wpdb-&gt;commentmeta.meta_key = '_commentsvote' AND $wpdb-&gt;comments.comment_date &gt;='" . $current_year . "-" . $current_month . "-01' AND $wpdb-&gt;comments.comment_date &lt;='" . $current_year . "-" . $current_month . "-31' ORDER BY CONVERT(INT, $wpdb-&gt;commentmeta.meta_value) DESC CAST SELECT $wpdb-&gt;commentmeta.meta_value, $wpdb-&gt;comments.comment_ID, $wpdb-&gt;comments.comment_date, $wpdb-&gt;comments.user_id, $wpdb-&gt;comments.comment_content, $wpdb-&gt;comments.comment_author FROM $wpdb-&gt;commentmeta JOIN $wpdb-&gt;comments ON $wpdb-&gt;comments.comment_ID = $wpdb-&gt;commentmeta.comment_id WHERE $wpdb-&gt;commentmeta.meta_key = '_commentsvote' AND $wpdb-&gt;comments.comment_date &gt;='" . $current_year . "-" . $current_month . "-01' AND $wpdb-&gt;comments.comment_date &lt;='" . $current_year . "-" . $current_month . "-31' ORDER BY CAST($wpdb-&gt;commentmeta.meta_value AS INT) DESC </code></pre> <p>I keep getting sql errors on both of these when i run it as a sql statement (obviously, i convert the $wpdp to just say wp_) out of wordpress. the error is non specific - i.e. "you have an error near as int" for example.</p> <p>so, two questions, first one is wp specific, and the second is a sql:</p> <ol> <li>i'm pretty sure there's a way to write all this as an arg array i can pass into wp_query, but i'm unsure of how to do a join. code examples here would be super helpful.</li> <li>why why WHY is this sql statement unwilling to convert that last colomn to an int?</li> </ol> <p>Thanks Wp and SQL geniuses for reading!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload