我的SQL查询从两个表中获取信息

时间:2018-07-09 15:54:13

标签: php mysql

我一直在尝试为我的画廊制作一个新的小部件,它可以向我显示最近更新的相册。最后上传的图像始终具有较高的pid,并且这些图像只能位于一个相册中。

但是长时间尝试做某事后,我做了一些事,但遇到两个问题: 第一:没有专辑标题。 第二:它没有显示我选择的拇指。

这是我的代码

git push --force

在cpgp7_pictures中,您将找到:“ pid”,“ aid”,“ filepath”,“ filename”等。 在cpgp7_albums中,您会找到:“ aid”,“ title”,“ thumb”(封面的pid)

示例:

<?php
    require_once('include/config.inc.php');
    header("Content-type: application/x-javascript");

    $connect = mysql_connect('localhost','user','pass') or die('Error conexion server');
    $connect_db = mysql_select_db('database', $connect) or die ('Error conexion base de datos');

                $resultado = mysql_query(" SELECT DISTINCT(aid) FROM cpgq7_pictures ORDER BY pid DESC LIMIT 0 ,6", $connect) or die('Ningun album encontrado');

                echo 'document.write(\'';

                    if(mysql_num_rows($resultado) == 0){
                        echo 'Ningun album obtenido';
                    } else {

echo '<div class="photos">  ';
                        while($row = mysql_fetch_array($resultado)){
                            echo ' ';
                                $album_id = $row['aid'];
                                $subresult = mysql_query("SELECT * FROM cpgq7_pictures where aid=$album_id order by pid DESC LIMIT 0, 6");
                                $album_title = mysql_query("SELECT * FROM cpgq7_albums where aid=$album_id DESC LIMIT 0, 6");

                                if(mysql_num_rows($subresult) == 0){
                                    $album_img = "http://arianagrandechile.net/galeria/thumbs/thumb_nopic.png";
                                } else {
                                    while($subrow = mysql_fetch_array($subresult)){
                                        $album_img = "http://arianagrandechile.net/galeria/albums/".$subrow['filepath'].'normal_'.$subrow['filename']  .$subrow['datebrowse'];
                                    }
                                }
                                echo '<div class="g-album">';
                                    echo '<a href="http://arianagrandechile.net/galeria/thumbnails.php?album='.$album_id.' " target="_blank"><img src="'.$album_img.'" alt="" /></a>';
                                    echo '<div class="g-title"><a href="http://arianagrandechile.net/galeria/thumbnails.php?album='.$album_id.' " target="_blank">'.$album_title.'</a></div>';
                                echo "</div>";
                        }
                        echo '</div>';
                    }
                    echo '\');';
            ?>

加载的窗口小部件应类似于:

cpgp7_pictures

pid       aid    filepath          filename
21074     159    userpics/10002/   CREATIONS00004.jpg
21073     405    userpics/10002/   LMH00003.jpg
21072     405    userpics/10002/   LMH00002.jpg
21071     405    userpics/10002/   LMH00001.jpg

cpgp7_albums

aid       title                                    thumb
405       T-Mobile, Las Vegas - CreationsOfLa      21074
159       Love Me Harder - Jones Crow              21071

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

与其在PHP中进行联接,不如使用单个SQL查询来获取数据。

SELECT * 
FROM `cpgq7_pictures` a
JOIN `cpgq7_albums` b
    ON a.`aid` = b.`aid`
GROUP BY a.`aid`, a.`pid`
ORDER BY a.`aid`, a.`pid` DESC 
LIMIT 0, 6

您无需限制cpgq7_albums中的行,因为每张照片只应匹配一张相册。

编辑 创建了一个SQLFiddle以显示查询

SQL Fiddle

MySQL 5.6模式设置

CREATE TABLE IF NOT EXISTS `cpgp7_pictures` (
    `pid`       INT(11) UNSIGNED        NOT NULL    AUTO_INCREMENT  COMMENT 'Primary Key',
    `aid`       INT(11) UNSIGNED        NULL        DEFAULT NULL,
    `filepath`  VARCHAR(200)            NOT NULL    DEFAULT '',
    `filename`  VARCHAR(200)            NOT NULL    DEFAULT '',
    PRIMARY KEY (`pid`)
) 
    ENGINE=MyISAM 
    AUTO_INCREMENT=1 
    DEFAULT CHARSET=utf8 
    COLLATE=utf8_unicode_ci
    COMMENT '';

CREATE TABLE IF NOT EXISTS `cpgp7_albums` (
    `aid`       INT(11) UNSIGNED        NOT NULL    AUTO_INCREMENT  COMMENT 'Primary Key',
    `title`     VARCHAR(200)            NOT NULL    DEFAULT '',
    `thumb`     INT(11) UNSIGNED        NULL        DEFAULT NULL,
    PRIMARY KEY (`aid`)
) 
    ENGINE=MyISAM 
    AUTO_INCREMENT=1 
    DEFAULT CHARSET=utf8 
    COLLATE=utf8_unicode_ci
    COMMENT '';



INSERT INTO `cpgp7_pictures`
(`pid`,`aid`,`filepath`,`filename`)
VALUES
(21074,159,'userpics/10002/','CREATIONS00004.jpg'),
(21073,405,'userpics/10002/','LMH00003.jpg'),
(21072,405,'userpics/10002/','LMH00002.jpg'),
(21071,405,'userpics/10002/','LMH00001.jpg');

INSERT INTO `cpgp7_albums`
(`aid`,`title`,`thumb`)
VALUES
(405,'T-Mobile, Las Vegas - CreationsOfLa',21074),
(159,'Love Me Harder - Jones Crow',21071)

查询1

SELECT * 
FROM `cpgp7_pictures` a
JOIN `cpgp7_albums` b
    ON a.`aid` = b.`aid`
GROUP BY a.`aid`, a.`pid`
ORDER BY a.`aid`, a.`pid` DESC 
LIMIT 0, 6

Results

|   pid | aid |        filepath |           filename | aid |                               title | thumb |
|-------|-----|-----------------|--------------------|-----|-------------------------------------|-------|
| 21074 | 159 | userpics/10002/ | CREATIONS00004.jpg | 159 |         Love Me Harder - Jones Crow | 21071 |
| 21073 | 405 | userpics/10002/ |       LMH00003.jpg | 405 | T-Mobile, Las Vegas - CreationsOfLa | 21074 |
| 21072 | 405 | userpics/10002/ |       LMH00002.jpg | 405 | T-Mobile, Las Vegas - CreationsOfLa | 21074 |
| 21071 | 405 | userpics/10002/ |       LMH00001.jpg | 405 | T-Mobile, Las Vegas - CreationsOfLa | 21074 |

第二次编辑

用于运行查询和输出div的PHP代码。

<?php
    $mysqli = new mysqli($host,$user,$pass,$dbname);
    if($mysqli->connect_errno) {
        echo "<p>Error connecting to db: ".$mysqli->connect_errno.", ".$mysqli->connect_error."</p>\n";
    } else {
        $qstr = "";
        $qstr .= "SELECT\n";
        $qstr .= "  a.`aid`,\n";
        $qstr .= "  a.`pid`,\n";
        $qstr .= "  a.`filepath`,\n";
        $qstr .= "  a.`filename`,\n";
        $qstr .= "  b.`title`,\n";
        $qstr .= "  b.`thumb`\n";
        $qstr .= "FROM `cpgp7_pictures` a\n";
        $qstr .= "JOIN `cpgp7_albums` b\n";
        $qstr .= "    ON a.`aid` = b.`aid`\n";
        $qstr .= "GROUP BY a.`aid`\n";
        $qstr .= "ORDER BY a.`aid`\n";
        $qstr .= "LIMIT 0, 6;";
        $results = $mysqli->query($qstr);
        if($results) {
            while($row = $results=>fetch_assoc()) {
                echo "<div class='photos'>\n";
                echo "    <div class='g-album'>\n";
                echo "    <a href='http://arianagrandechile.net/galeria/thumbnails.php?album=".$row['aid']."' target='_blank'><img src='http://arianagrandechile.net/galeria/albums/".$row['filepath']."normal_".$row['filename']."' alt='' /></a>\n";
                echo "    <div class='g-title'><a href='http://arianagrandechile.net/galeria/thumbnails.php?album=$row['aid']' target='_blank'>".$row['title']."</a></div>\n";
                echo "</div>\n";
            }
        }
    }
?>