将两个SELECT连接成一个mysql_query

时间:2013-03-03 08:37:39

标签: php mysql

有问题,我需要加入2个SELECT。我有这个代码,但它将所有内容从$ age放到页面末尾,我需要它与其他resutls在同一行中

<?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 个答案:

答案 0 :(得分:1)

你可以这样做:

mysql_query("SELECT *, DATEDIFF( CURRENT_DATE, founded ) as date_difference FROM business");

答案 1 :(得分:1)

select a, b, a-b as difference from mytable

答案 2 :(得分:0)

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

答案 3 :(得分: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();
?>