2013-10-30 63 views
0

我想在插入前選擇前10行。如果插入前沒有行,那麼它只是返回已插入的行mysql插入並選擇帶插入標識的行

插入後,我可以通過使用 mysql_insert_id();

在插入之前選擇前10行是一種智能的方法。

這必須適用於區間0或10內的任何數字。所以假設它們在插入之前是7行,那麼它只會返回8行。

我在考慮使用限制的,像這樣

$insertid = mysql_insert_id() 
    $postid = 200 
    Select a, b, c from mytable where a = '$postid' limit $insertid 10 order by b 

但這不起作用像我承擔。

+0

你是指插入10行在這一行之前?基於某些列ID的10行?或者是其他東西? – Kairan

+0

否這行不能插入10行。這10行基於變量$ postid。我編輯了我的問題 –

+0

沒有ORDER BY的LIMIT沒有多大意義。 「其他」行之前的「某些」行是什麼意思? – Strawberry

回答

1
Select a, b, c from mytable where primary_key_col < '$insertid' and a = 200 
ORDER BY primary_key_col DESC LIMIT 10 
+0

對不起,我編輯了我的問題。您的答案會返回變量insertid之前的所有行,但我試圖只將列a的行作爲200. –

+0

根據您的新要求進行更新。 –

+0

爲了選擇插入的註釋,我使用了從mytable中選擇a,b,c,其中primary_key_col <='$ insertid'和a = 200 ORDER BY primary_key_col DESC LIMIT 10 –

0

您之間可以使用:

Select a, b, c from mytable where a BETWEEN $postid - 10 AND $postid 

(順序事項)

此外,你應該如何使用,而不是MySQL的mysqli_事情