doutouman6245 2017-11-10 10:10
浏览 32
已采纳

我正在尝试根据其他2个表中的信息计算第三个表中的信息

I'm trying to count information in a third table based on info from 2 other tables in 1 single SELECT statement while on a mission to simplify my code as much as posible

I have 3 tables

video

-----------------------------------------------------------
| id |  date  | userID | ... | title | ... | randomString |
-----------------------------------------------------------
| 1  | <date> |   1    |     | This  |     | d45df6gdf4f5 |
|    |        |        |     | is a  |     |              |
|    |        |        |     | great |     |              |
|    |        |        |     | video |     |              |
-----------------------------------------------------------
| 2  | <date> |   5    |     | This  |     | fdg45f65g4df |
|    |        |        |     | is an |     |              |
|    |        |        |     | even  |     |              |
|    |        |        |     | better|     |              |
|    |        |        |     | video |     |              |
-----------------------------------------------------------
| 3  | <date> |   1    |     | Not so|     | sdf7d89sd87f |
|    |        |        |     | good  |     |              |
|    |        |        |     | video |     |              |
-----------------------------------------------------------
| 4  | <date> |   2    |     | Even  |     | hgj54ghjhg6h |
|    |        |        |     | worse |     |              |
|    |        |        |     | video |     |              |
-----------------------------------------------------------
| 5  | <date> |   2    |     | Now we|     | 45j43kj4klkj |
|    |        |        |     | are   |     |              |
|    |        |        |     | scrapi|     |              |
|    |        |        |     | ng    |     |              |
|    |        |        |     | rock  |     |              |
|    |        |        |     | bottom|     |              |
-----------------------------------------------------------
.
.
.
-----------------------------------------------------------
| n  |        | user-n |     | tit-n |     |    ran-n     |
-----------------------------------------------------------

(video with id 1 was uploaded by user with id 1 in the below table)
(video with id 2 was uploaded by user with id 5)
(video with id 3 was uploaded by user with id 1)
Etc.

userinfo

--------------------------------------------
| id | ... | username | ... | randomString |
--------------------------------------------
| 1  |     | Bob      |     | kj4h5k34jh5k |
--------------------------------------------
| 2  |     | Alice    |     | 8sadsa76asd6 |
--------------------------------------------
| 3  |     | Ned      |     | kj23423kjkqw |
--------------------------------------------
| 4  |     | Jill     |     | sd7sdfhgsdmn |
--------------------------------------------
| 5  |     | John     |     | 5sdfsd4df7s5 |
--------------------------------------------
.
.
.
--------------------------------------------
| n  |     | name-n   |     |    ran-n     |
--------------------------------------------

(user with id 1 has username Bob)
(user with id 2 has username Alice)
Etc.

videoview

-------------------------
| id | userID | videoID |
-------------------------
| 1  |   5    |    3    |
-------------------------
| 2  |   2    |    3    |
-------------------------
| 3  |   1    |    3    |
-------------------------
| 4  |   2    |    1    |
-------------------------
| 5  |   3    |    2    |
-------------------------
.
.
.
-------------------------
| n  | user-n | video-n |
-------------------------

(user with id 5 viewed video with id 3 from the above tables)
(user with id 2 viewed video with id 3 from the above tables)
(user with id 1 viewed video with id 3 from the above tables)
(user with id 2 viewed video with id 1 from the above tables)
Etc.

What I need is to get the viewcount from all the videos

So in this case the result should be
1 for video 1 (video with id 1 viewed 1 time)
1 for video 2 (video with id 2 viewed 1 time)
3 for video 3 (video with id 3 viewed 3 times)
Etc.

connection.php

try {

    $servername = "localhost";
    $username = "*****";
    $password = "*********";
    $database = "****";
    $charset = 'utf8';

    $dsn = "mysql:host=$servername;dbname=$database;charset=$charset";

    $opt = [
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES   => false,
    ];

    $pdo = new PDO ( $dsn , $username , $password , $opt );

} catch ( PDOException $e ) {

    file_put_contents ( 'PDOError.txt', $e->getMessage(), FILE_APPEND );
    echo "Failed to connect to database!";
    die ();

}

Code that works but is way to cumbersome

functions.php

try {

    $prepareVideo = $pdo->prepare( "
        SELECT v.date AS vDate , v.id AS vVideoID , v.userID AS vUserID , v.randomString AS vRandomString , v.title AS vTitle , u.username AS uUsername , u.randomString AS uRandomString 
        FROM video AS v 
        LEFT JOIN userlogininfo AS u ON v.userID = u.id 
        ORDER BY v.recommended DESC 
        LIMIT ?
    " );
    $prepareVideo->execute([$limit]);
    $getVideo = $prepareVideo->fetchAll();

    $countVideo = count ( $getVideo );

} catch ( PDOException $e ) {

    file_put_contents ( 'error.txt', $e->getMessage(), FILE_APPEND );

}

if ( $getVideo ) {

    // get needed variables

    try {

        $prepareVideoView = $pdo->prepare( "SELECT id FROM videoview WHERE videoID = ?" );
        $prepareVideoView->execute([$vVideoID]);
        $getVideoView = $prepareVideoView->fetchAll();

        $countVideoView = count ( $getVideoView );

    } catch ( PDOException $e ) {

        file_put_contents ( 'error.txt', $e->getMessage(), FILE_APPEND );

    }

}

But this is way to much code for this

What I want to do is also count the viewcount in 1 single SELECT statement, like

functions.php

try {

    $prepareVideo = $pdo->prepare( "SELECT..." );
    $prepareVideo->execute([]);
    $getVideo = $prepareVideo->fetchAll();

    $countVideo = count ( $getVideo );

} catch ( PDOException $e ) {

    file_put_contents ( 'error.txt', $e->getMessage(), FILE_APPEND );

}

if ( $getUser ) {

    // get all the info I need

}

Insert single SELECT statement that does the same as above

What I have tried so far

// add another LEFT JOIN
SELECT... , vv.videoID AS vvVideoID FROM video ... LEFT JOIN videoview AS vv ON vv.videoID = v.id ...

but that just lists info for video 1 - 1 time, info for video 2 - 1 time, info for video 3 - 3 times, etc. giving me way to many entries

And that's not what I want

I've also tried adding a count(*) in there but that just throws a $getVideo undefined out there

Hope someone can help

  • 写回答

2条回答 默认 最新

  • douke3335 2017-11-10 10:23
    关注

    One way you could do this is via subquery:

    SELECT *, (SELECT COUNT(*) FROM videoview WHERE `videoID` = `video`.`id`) as `views` FROM video
    

    This will add the field views to the output of the video table.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重