在 SQL 資料庫中選擇隨機記錄
資料庫查詢通常針對特定行。 但是如何有效地檢索完全隨機的行呢? 不同資料庫系統的方法略有不同:
MySQL:
<code class="language-sql">SELECT column FROM table ORDER BY RAND() LIMIT 1</code>
PostgreSQL:
<code class="language-sql">SELECT column FROM table ORDER BY RANDOM() LIMIT 1</code>
Microsoft SQL Server:
<code class="language-sql">SELECT TOP 1 column FROM table ORDER BY NEWID()</code>
IBM DB2:
<code class="language-sql">SELECT column, RAND() as IDX FROM table ORDER BY IDX FETCH FIRST 1 ROWS ONLY</code>
甲骨文:
<code class="language-sql">SELECT column FROM ( SELECT column FROM table ORDER BY dbms_random.value ) WHERE rownum = 1</code>
這些範例示範如何取得單一隨機行。 請記得將 column
和 table
替換為您的實際列名和表名。
以上是如何從 SQL 資料庫中選擇隨機行?的詳細內容。更多資訊請關注PHP中文網其他相關文章!