获取SQL查询的行号

时间:2018-06-28 19:14:23

标签: sql wordpress

我有这个WP_Query($arg)

$arg = array(
    'post_type' => 'ma_novel',
    'post_status'=>'fn_publish',
    'posts_per_page' => 10,
    'order' => 'DESC',
    'orderby' => 'meta_value_num',
);

执行此SQL:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
WHERE 1=1
  AND (wp_postmeta.meta_key = 'ma_rating')
  AND wp_posts.post_type = 'fn_novel'
  AND ((wp_posts.post_status = 'publish'))
GROUP BY wp_posts.ID
ORDER BY wp_postmeta.meta_value+0 DESC
LIMIT 0,
      10

我的目的是获取帖子ID为108的帖子的行号。

到目前为止,我有这个:

SELECT allposts.*
FROM
  ( SELECT @rownum := @rownum + 1 AS rownum, posts.*
   FROM
     ( SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
      FROM wp_posts
      INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
      WHERE 1=1
        AND (wp_postmeta.meta_key = 'ma_rating')
        AND wp_posts.post_type = 'fn_novel'
        AND ((wp_posts.post_status = 'publish'))
      GROUP BY wp_posts.ID
      ORDER BY wp_postmeta.meta_value+0 DESC ) AS posts,

     (SELECT @rownum := 0) AS r ) AS allposts
WHERE allposts.ID=108

但是它不起作用。获取行号我做错了什么?我非常有信心这个问题在这里:

SELECT @rownum := @rownum + 1 AS rownum, posts.*
       FROM

AS allposts
    WHERE allposts.ID=108

如何获取ID为108的行号?

0 个答案:

没有答案