简体   繁体   中英

Can I randomly select a row from one table and use a static identifier to select corresponding data in another table?

The following script was initially intended to select a row at random, which it could do fine, however, I an unable to figure out how to amend it to select data from a second table using a column ID called "nid" which is the same in both tables:

    $conn = mysqli_connect($host,$username,$password, $database) or die(mysql_error ()); 


$total_rows = 5;
$selected_row = mt_rand(0, $total_rows);


$query="SELECT * FROM `node` LIMIT $selected_row, 1;";
$result=$conn->query($query);

while($row = $result->fetch_assoc()) {
$node=$row["nid"];
echo $row["title"];

$query2="SELECT * FROM `field_data_body` LIMIT $node, 1;";
$result2=$conn->query($query2);

while($row = $result->fetch_assoc()) {
echo $row["body_value"];

 }

Admittedly, the above is not really close to working, but I was hardpressed to find an example of what I was after.

These tables are in the database for a Drupal site, so the title field and the body_value fields are in two different tables; ultimately, I would like to echo a result that is a matching set of title and body_value for a randomly selected node.

Speaking of this script specifically, I want to use the nid to find the corresponding row for the second table.

Is this possible?

The bit that works, selecting the data I want from a single table is in the following format:

    $total_rows = 5;
$selected_row = mt_rand(0, $total_rows);


$query="SELECT * FROM `field_data_body` LIMIT $selected_row, 1;";
$result=$conn->query($query);

while($row = $result->fetch_assoc()) {
echo $row["body_value"];

 }
?>

UPDATE:

At the suggestion of a commenter, I used a join, and ended up with:

$conn = mysqli_connect($host,$username,$password, $database) or die(mysql_error ()); 


$total_rows = 5;
$selected_row = mt_rand(0, $total_rows);

//Use the result in your limit.
$query="SELECT a.nid, a.title, b.entity_id, b.body_value
FROM node a, field_data_body b
WHERE a.nid = b.entity_id LIMIT $selected_row, 1;";
$result=$conn->query($query);

while($row = $result->fetch_assoc()) {
echo $row["title"];
echo " | ";
echo $row["body_value"];

 }

which worked perfectly.

for future reference since you have solved your problem, you need to close the previous connection before starting a new one, or free the results

$conn = mysqli_connect($host,$username,$password, $database) or die(mysql_error ()); 


$total_rows = 5;
$selected_row = mt_rand(0, $total_rows);


$query="SELECT * FROM `node` LIMIT $selected_row, 1;";
$result=$conn->query($query);

while($row = $result->fetch_assoc()) {
$node=$row["nid"];
echo $row["title"];
$result->close(); // close $result

$query2="SELECT * FROM `field_data_body` LIMIT $node, 1;";
$result2=$conn->query($query2);

while($row = $result->fetch_assoc()) {
echo $row["body_value"];
 }
$result2->close(); // close $result2

i also notice you haven't closed the connection on the UPDATE you posted either which will lead you to run into the same problem again later on.

如果这只是选择一条记录,则不必定义LIMIT,而是可以在WHERE中使用随机生成的值来选择该行!

You can just join the two tables. Beside that, your code has a potential problem. It relies on the existence of at least 5 records. You can avoid that by putting the randomization into the query. The code will then be

$conn = mysqli_connect($host,$username,$password, $database) or die(mysql_error()); 

$query  = "SELECT n.*, b.* FROM `node` AS n LEFT JOIN `field_data_body` AS b ON n.nid=b.nid ORDER BY REVERSE(RAND()) LIMIT 0, 1;";
$result = $conn->query($query);
$row    = $result->fetch_assoc();
$result->close();

echo $row["title"];
echo $row["body_value"];

The REVERSE() function makes the result from RAND() more random. You may omit it, if you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM