2017-10-09 65 views
0

我在想這個「問題」無法解決。 我在我的代碼不短 querys。order by pid? (asc | desc)佔位符聲明

根據變量的值,我需要通過ASC或DESC對日期進行排序。

我不想copypaste查詢。我認爲我可以照常工作,但事實並非如此。 (?)

如何能(是否可能在所有)替換查詢ASC或DESC,以及變量?

query prepare error 
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? limit ? offset ?' at line 1 

simply example:

... 
$sort='asc'; 
... 
select pid,title_posts,txt_posts,count_postvs,usname from posts join usopt on uid_posts=uid_usopt 
where uid_posts=? and postacc=1 order by pid ? limit ? offset ? 

mysqli_stmt_bind_param($rawdata, 'isii', $uid, $sort, $_SESSION['r2sh'], $offset); 
... 

回答

0

可以使用在order by條件表達式做到這一點:

order by (case when ? = 'asc' then pid end) asc, 
     pid desc 
+0

有趣......我就知道這麼一點關於MySQL :( –