MYSQL/PHP: How do you Order By Starting point in a string?
SELECT
SQL_CALC_FOUND_ROWS posts.*,
CASE
WHEN postmeta.meta_value REGEXP '$regex'
THEN 1
ELSE 0
END AS keyword_in_title,
MATCH ( posts.post_content )
AGAINST ( '".addslashes( $s )."' ) AS mysql_score
FROM
$wpdb->posts as posts,
$wpdb->postmeta as postmeta
WHERE
posts.post_date_gmt <= now()
AND postmeta.post_id = posts.ID
AND postmeta.meta_key='_headspace_page_title'
AND posts.post_password = ''
AND posts.post_status = 'pub开发者_如何学编程lish'
AND posts.post_type != 'attachment'
AND ( postmeta.meta_value REGEXP '$regex'
OR posts.post_content REGEXP '$regex')
GROUP BY
posts.ID
ORDER BY
charindex($s, 'keyword_in_title') DESC
LIMIT
$offset,
$limit
As to the order issue (Apart from the escaping issue @Gumbo pointed out in the comment), CHARINDEX
is not a valid mysql string function. The LOCATE
function looks to be identical (at least from a cursory glance between the docs on SQLServer and MySQL)...
$s = mysql_real_escape_string($s);
$order = 'ORDER BY LOCATE(\''.$s.'\', `keyword_in_title`) DESC';
I'm assuming that the code above does not work and hence you're asking a question.
Try adding charindex($s, 'keyword_in_title') AS cOrder
in your SELECT
clause and then ORDER BY cOrder DESC
Good luck!
精彩评论