search

Home  >  Q&A  >  body text

php - Why can't the sql statement extract the ID and title fields, but only the title field?

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

扔个三星炸死你扔个三星炸死你2781 days ago666

reply all(2)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-06-10 09:49:31

    Replace * with t1.* and see

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-06-10 09:49:31

    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.

    reply
    0
  • Cancelreply