php - Why can't the sql statement extract the ID and title fields, but only the title field?
扔个三星炸死你
扔个三星炸死你 2017-06-10 09:47:31
0
2
629

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

扔个三星炸死你
扔个三星炸死你

reply all(2)
淡淡烟草味

Replace * with t1.* and see

过去多啦不再A梦

First: 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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template