1

我有下面列出的 3 个单独的 MySQL 查询,有什么方法可以组合它们,这样我就可以获得相同的输出,但只需调用一次数据库(而不是 3 个)

提前致谢

    <?php  
$host   = ''; 
$dbUser = '';   
$dbPass = ''; 
$db = ''; 
mysql_connect("$host", "$dbUser", "$dbPass") or die(mysql_error());
mysql_select_db("$db") or die(mysql_error());

$result = mysql_query("SELECT id,cat,extra1,bigimage FROM pp_photos WHERE extra1 = '120' ORDER BY rand() LIMIT 1" ) or die(mysql_error());   
while($row = mysql_fetch_array( $result )) {    
print "<a href=\"http://www.\" target=\"_new\"> <img src=\"ng\"  border=\"0\" ></a>";
}
?> 
<?php  
$host   = ''; 
$dbUser = '';   
$dbPass = ''; 
$db = ''; 
mysql_connect("$host", "$dbUser", "$dbPass") or die(mysql_error());
mysql_select_db("$db") or die(mysql_error());

$result = mysql_query("SELECT id,cat,extra1,bigimage FROM pp_photos WHERE extra1 = 'W599_PTO' ORDER BY rand() LIMIT 1" ) or die(mysql_error());   
while($row = mysql_fetch_array( $result )) {    
print "<a href=\"php">[?]</FONT></a>";
}
?>
<?php  
$host   = ''; 
$dbUser = '';   
$dbPass = ''; 
$db = ''; 
mysql_connect("$host", "$dbUser", "$dbPass") or die(mysql_error());
mysql_select_db("$db") or die(mysql_error());

$result = mysql_query("SELECT id,cat,extra1,extra5,bigimage FROM pp_photos WHERE extra5 = '120rear' ORDER BY rand() LIMIT 1" ) or die(mysql_error());   
while($row = mysql_fetch_array( $result )) {    
print "<a href=\"09\" target=\"_new\"> <img src=\"http://pg\"  border=\"0\" ert\"></a>";
}
?> 

我现在已经根据下面提供的帮助编辑了代码......我是 MySQL 的新手,我不确定如何设置过滤器,以便根据“额外 1”字段的值显示任何或所有 3 张图像...

    <?php  
$host   = ''; 
$dbUser = '';   
$dbPass = ''; 
$db = ''; 
mysql_connect("$host", "$dbUser", "$dbPass") or die(mysql_error());
mysql_select_db("$db") or die(mysql_error());

$result = mysql_query("
SELECT extra1 
FROM pp_photos 
WHERE extra1 IN('1','10','12') 
GROUP BY extra1" ) 
or die(mysql_error());   
while($row = mysql_fetch_array( $result )) {    
print "<a href=\"h3\" target=\"_new\"> images</a>";
}
?> 
4

2 回答 2

1

使用UNION怎么样

$query = "
SELECT id,cat,extra1,bigimage FROM pp_photos WHERE extra1 = '120' ORDER BY rand() LIMIT 1
UNION
SELECT id,cat,extra1,bigimage FROM pp_photos WHERE extra1 = 'W599_PTO' ORDER BY rand() LIMIT 1
UNION
SELECT id,cat,extra1,bigimage FROM pp_photos WHERE extra1 = '120rear' ORDER BY rand() LIMIT 1
";
$cats = array(
    '120'      => '503',
    'W599_PTO' => '508',
    '120rear'  => '509'
);
$result = mysql_query($query) or die(mysql_error());   
while($row = mysql_fetch_array( $result )) {    
    print '<a href="http://www.nctfleetlist.co.uk/photos/search.php?keywords='.
          $row['extra1'] .
          '&cat=' .
          $cats[$row['extra1']] .
          '" target="_new"><img src="http://www.nctfleetlist.co.uk/images/camera.png"  border="0" title="photos available"></a>';
}
于 2012-04-19T13:10:40.727 回答
1
SELECT id,cat,extra1,bigimage 
FROM pp_photos 
WHERE extra1 IN('W599_PTO','120','120rear') 
GROUP BY extra1

This will give you 3 rows. You need to filter according to some condition to get desired row from 3 rows, order by is not required

于 2012-04-19T12:15:04.940 回答