2013-03-03 65 views
0

有問題,我需要連接2個SELECTs。我有這樣的代碼,但它把所有從$年齡頁的末尾,我需要它在同一行其他resutls將兩個SELECT連接成一個mysql_query

<?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(); 
?> 

回答

1

你可以這樣說:

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

非常感謝你,它的工作! – ProfiThing 2013-03-03 08:49:09

1
select a, b, a-b as difference from mytable 
0
$result = mysql_query("SELECT *, DATEDIFF(CURRENT_DATE, founded) as date_difference FROM business"); 
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(); 
?>