我有下面列出的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的新手,我不确定如何设置过滤器,以便根据'额外的值显示任何或所有3个图像1'场......
<?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>";
}
?>
答案 0 :(得分:1)
SELECT id,cat,extra1,bigimage
FROM pp_photos
WHERE extra1 IN('W599_PTO','120','120rear')
GROUP BY extra1
这将为您提供3行。您需要根据某些条件进行过滤以从3行获得所需的行,不需要排序
答案 1 :(得分: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>';
}