SQL statement
public function get_radom_article(){
$table1 = $this->get_table('article') ;
$sql = 'SELECT *
FROM `'.$table1 .'` AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `'.$table1 .'`)-(SELECT MIN(id) FROM `'.$table1 .'`))+(SELECT MIN(id) FROM `'.$table1 .'`)) AS id) AS t2
WHERE t1.id >= t2.id
ORDER BY t1.id LIMIT 10';
return $this->query_all($sql);
}
extract
<?php foreach($this->radom_articles AS $key => $val) { ?>
<li><a href="article/<?php echo $val['id']; ?>" title="<?php echo $val['title']; ?>"><?php echo $val['title']; ?></a></li>
<?php } ?>
The title can be extracted but the ID cannot be extracted?
There are no errors in the titles of these extracted data, but why are all the IDs the same, that is, the ID of the corresponding title field is not extracted? What went wrong with SQL? Under the guidance of all the masters
The title field is title and the id field is id
Replace
*
witht1.*
and seeFirst: If t1.id >= t2.id in your join table query, duplicate data will already appear, although your algorithm seems to be less likely to do so.
Second: The result set contains multiple id fields. The last id field after the select in the result taken out by PHP will overwrite the previous value. Of course, the query optimizer may also change the order of the columns after the select.