将多个SQL查询合并为一个

时间:2012-07-09 20:57:54

标签: php mysql sorting while-loop

我知道这可能很简单,但我试图找到一些类似的例子并且失败了。

问题是我想列出数据库中图库的8个随机图像,按添加日期排序。我已经设法做到这一点,但只有几个迭代查询变得非常慢。因此,如果有人能够如此友好地教我将它们组合起来以加快速度,我想UNION是要走的路吗?这是我的工作(但是代码)

<?php       
        $latestPictures = mysql_query("SELECT pictureID, addedDate FROM picture ORDER BY addedDate DESC LIMIT 8"); 

        $latestConcerts = mysql_query("SELECT concertID, addedDate FROM concert WHERE pictureID is null ORDER BY addedDate DESC LIMIT 8"); 


        // Add concerts and pictures to array
        while($curFestival = mysql_fetch_object($latestPictures))
        {
            $array[$curFestival->addedDate] = "p" . $curFestival->pictureID;
        }

        while($curConcert = mysql_fetch_object($latestConcerts))
        {
            $array[$curConcert->addedDate] = "c" . $curConcert->concertID;
        }

        // Order array by key
        krsort($array);

        $latestArray = array_slice($array, 0, 8);

        foreach($latestArray as $key => $value) {

            $type = substr($value, 0, 1);
            $ID = substr($value, 1);

            // If type == picture
            if($type == 'p')
            {           
                $picturesPicturesID = mysql_query("SELECT concertID, name FROM photo WHERE concertID IN(SELECT concertID FROM concert WHERE pictureID = $ID) ORDER BY photoID");

                // Get random picture
                $curRandomPicture = rand(0, (mysql_num_rows($picturesPicturesID) - 1));
                $curPictureConcertID = mysql_result($picturesPicturesID, $curRandomPicture, "concertID");
                $curPictureName = mysql_result($picturesPicturesID, $curRandomPicture, "name");             
                $curPicture = mysql_fetch_object(mysql_query("SELECT c.URL, p.name FROM concert c, picture p WHERE p.pictureID = c.pictureID AND c.concertID = $curPictureConcertID")); 

             echo "Some image"; 
             }
            // If type == concert
            if($type == 'c')
            {
                $concertPicturesID = mysql_query("SELECT concertID, name FROM photo WHERE concertID = $ID ORDER BY photoID");

                // Get random picture
                $curRandomPicture = rand(0, (mysql_num_rows($concertPicturesID) - 1));  
                $curPictureConcertID = mysql_result($concertPicturesID, $curRandomPicture, "concertID");
                $curPictureName = mysql_result($concertPicturesID, $curRandomPicture, "name");              
                $curPicture = mysql_fetch_object(mysql_query("SELECT URL, name FROM concert WHERE concertID = $curPictureConcertID")); 

            echo "Some image";
            }
        }
?>

我意识到我忘了包括表格,这里是:

TABLE OF photo:
PKEY: photoID
FKEY: concertID
name

TABLE OF concert:
PKEY: concertID
FKEY: pictureID
name
URL
addedDate

TABLE OF picture
PKEY: pictureID
name
date

所以每个帖子都是TABLE照片和音乐会的一部分,但只有一些是图片的一部分,女巫有时只用于将不同的专辑组合在一起。当他们被组合在一起时,我会从该分组ID(图片)中随机发一个名字帖子,如果他们是自己的随机名称帖子(音乐会)。

1 个答案:

答案 0 :(得分:0)

Accckkk! (在比尔猫的臭名昭着的话语中。)

很难从数据库中找出代码真正想要的结果集。

此查询不是最有效的,但它会从photos表中返回8个随机行,这些行按addedDate排序:

SELECT r.*, c.*
  FROM (SELECT p.*
          FROM photo p
         WHERE p.concertid IS NOT NULL
         ORDER BY RAND()
         LIMIT 0,8
       ) r
  JOIN concert c
    ON c.concertid = p.concertid
 ORDER BY r.addedDate ASC

如果你有一个非常大的photo表,那么这将是,因为必须为表中的每一行调用RAND()函数,并且MYSQL必须生成一个临时结果集(表的副本),然后在该派生列上对其进行排序。

(注意:我在这里假设你要从photo表返回“随机”行,我假设concertidconcert的主键。 1}}表,以及来自photo表的外键。显然你有三个表...... concertpicturephoto,但目前还不清楚哪些列是主键,哪些列是外键,所以我可能错了。)

(注意:将p.*c.*替换为您实际想要返回的表达式列表。)

返回单个随机行的方法更为有效。在您的情况下,您只需要八行,并且您可能不希望返回重复行。