0

有问题,我需要加入 2 个 SELECT。我有这个代码,但它把所有从 $age 到页面末尾,我需要它与其他结果在同一行

<?php
mysql_connect("***", "***", "***") or die(mysql_error());
mysql_select_db("***");

$result = mysql_query("SELECT * FROM business");
$age = mysql_query("SELECT DATEDIFF( CURRENT_DATE, founded ) as date_difference FROM business");

while($row = mysql_fetch_array($result))
  {
 ?><li class="onebusiness <?php echo $row['category']?>"><a href="<?php
  echo $row['link'];?>"><img src="<?php
  echo $row['img'];?>" height="125" width="125"/><p class="name"><?php
  echo $row['name'];?></p><p class="age"><?php
  echo $row['founded'];?></p></a></li><?php
  }

while($row = mysql_fetch_array($age))
  {
 ?><p class="age"><?php echo $row['date_difference']?></p><?php
  }
mysql_close();
?> 
4

4 回答 4

1

你可以这样做:

mysql_query("SELECT *, DATEDIFF( CURRENT_DATE, founded ) as date_difference FROM business");
于 2013-03-03T08:40:42.473 回答
1
select a, b, a-b as difference from mytable
于 2013-03-03T08:40:43.983 回答
0
$result = mysql_query("SELECT *, DATEDIFF( CURRENT_DATE, founded ) as date_difference FROM business");
于 2013-03-03T08:41:07.250 回答
0

你为什么不做

SELECT link, img, name, founded, DATEDIFF( CURRENT_DATE, founded ) as date_difference FROM business

这将为您提供所需的所有字段

<?php
    mysql_connect("***", "***", "***") or die(mysql_error());
    mysql_select_db("***");

    $result = mysql_query("SELECT `link`, `img`, `name`, `founded`, DATEDIFF( CURRENT_DATE, founded ) as 'date_difference' FROM `business`");

    while($row = mysql_fetch_array($result))
    {
    ?>
        <li class="onebusiness <?php echo $row['category']?>">
            <a href="<?php echo $row['link'];?>">
                <img src="<?php echo $row['img'];?>" height="125" width="125"/>
                <p class="name"><?php echo $row['name'];?></p>
                <p class="age"><?php echo $row['founded'];?></p>
            </a>
        </li>
        <p class="age"><?php echo $row['date_difference']?></p>
    <?php
    }
    mysql_close();
?> 
于 2013-03-03T08:41:21.360 回答