从记录MySQL-PHP中读取随机行

时间:2022-10-17 13:25:12

i will keep it simple to understand :

我会保持简单易懂:

1) i have a database with 2 columns - word and text

1)我有一个包含2列的数据库 - 单词和文本

2) Each word has approx. 600,000 lines of text associated with it.

2)每个单词都有约。与其相关的600,000行文本。

3) Iam a .net person shifting to php and mysql - so a little knowledge.

3)Iam一个.net人转移到php和mysql - 所以一点知识。

MY requirement :

我的要求:

1) I will pass the word through a form

1)我会通过表格传递这个词

2) The form will connect to the database and should display 2000 random lines from those 600,000 which should be non-repetitive

2)表单将连接到数据库,并应显示来自600,000的2000个随机行,这些行应该是非重复的

My current progress :

我目前的进展:

<?php
$con = mysql_connect("localhost","text_minx","pwd");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Data
WHERE word='health'");

while($row = mysql_fetch_array($result))
{
  echo $row['lines'];
  echo "<br />";
}
?>

This shows all the lines. What i want is to read this $row['lines'] in a possible array and randomly select 2000 lines from it - and they should be non-repetitive.

这显示了所有的行。我想要的是在一个可能的数组中读取这个$ row ['lines']并从中随机选择2000行 - 它们应该是非重复的。

Any help please?

有什么帮助吗?

3 个解决方案

#1


3  

Something like this:

像这样的东西:

$result = mysql_query("SELECT DISTINCT * FROM Data WHERE word='health' ORDER BY RAND() LIMIT 2000");

It's more efficient to select the 2000 random lines by MySQL instead of by PHP, as above.

如上所述,通过MySQL而不是PHP选择2000个随机行更有效。

Also note that SELECT DISTINCT will select only unique rows, which you should probably remove anyway. If you specify the column names instead of using * then you can choose which columns you want to be unique - although this also depends somewhat on how your table is built.

另请注意,SELECT DISTINCT将仅选择唯一的行,您可能应该删除它们。如果指定列名而不是使用*,则可以选择要唯一的列 - 尽管这在某种程度上取决于表的构建方式。

#2


0  

i think this is the way you want

我认为这是你想要的方式

SELECT * FROM Data WHERE word='health' ORDER BY RAND() LIMIT 0,2000

SELECT * FROM Data WHERE word ='health'ORDER BY RAND()LIMIT 0,2000

this will give you 2000 records sorted by any random order

这将为您提供按任意随机顺序排序的2000条记录

#3


0  

You need to split your row result into array (as it's currently a string), and then you can select 2000 lines randomly.

您需要将行结果拆分为数组(因为它当前是一个字符串),然后您可以随机选择2000行。

Assuming that the text has like breaks as line separators, it would look like that:

假设文本有像行分隔符一样的中断,它看起来像这样:

echo $row['lines']; //starts from this line of your code
$data = explode("\n", $row['lines']);
shuffle($data);
$random_lines = array_slice($data, 0, 2000);

This does not take care of non-repetition, though. If I understand your need correctly, you can use array_unique() function before passing it to shuffle().

但是,这并不需要考虑不重复。如果我理解你的需要,你可以在传递给shuffle()之前使用array_unique()函数。

#1


3  

Something like this:

像这样的东西:

$result = mysql_query("SELECT DISTINCT * FROM Data WHERE word='health' ORDER BY RAND() LIMIT 2000");

It's more efficient to select the 2000 random lines by MySQL instead of by PHP, as above.

如上所述,通过MySQL而不是PHP选择2000个随机行更有效。

Also note that SELECT DISTINCT will select only unique rows, which you should probably remove anyway. If you specify the column names instead of using * then you can choose which columns you want to be unique - although this also depends somewhat on how your table is built.

另请注意,SELECT DISTINCT将仅选择唯一的行,您可能应该删除它们。如果指定列名而不是使用*,则可以选择要唯一的列 - 尽管这在某种程度上取决于表的构建方式。

#2


0  

i think this is the way you want

我认为这是你想要的方式

SELECT * FROM Data WHERE word='health' ORDER BY RAND() LIMIT 0,2000

SELECT * FROM Data WHERE word ='health'ORDER BY RAND()LIMIT 0,2000

this will give you 2000 records sorted by any random order

这将为您提供按任意随机顺序排序的2000条记录

#3


0  

You need to split your row result into array (as it's currently a string), and then you can select 2000 lines randomly.

您需要将行结果拆分为数组(因为它当前是一个字符串),然后您可以随机选择2000行。

Assuming that the text has like breaks as line separators, it would look like that:

假设文本有像行分隔符一样的中断,它看起来像这样:

echo $row['lines']; //starts from this line of your code
$data = explode("\n", $row['lines']);
shuffle($data);
$random_lines = array_slice($data, 0, 2000);

This does not take care of non-repetition, though. If I understand your need correctly, you can use array_unique() function before passing it to shuffle().

但是,这并不需要考虑不重复。如果我理解你的需要,你可以在传递给shuffle()之前使用array_unique()函数。