我正在尝试根据1个单一SELECT语句中的其他2个表中的信息来计算第三个表中的信息,同时执行任务以简化我的代码和
我有3张桌子
视频
-----------------------------------------------------------
| 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 |
-----------------------------------------------------------
(id为1的视频由下表中ID为1的用户上传)
(id为2的视频由id为5的用户上传)
(id为3的视频由id为1的用户上传)
等
用户信息
--------------------------------------------
| id | ... | username | ... | randomString |
--------------------------------------------
| 1 | | Bob | | kj4h5k34jh5k |
--------------------------------------------
| 2 | | Alice | | 8sadsa76asd6 |
--------------------------------------------
| 3 | | Ned | | kj23423kjkqw |
--------------------------------------------
| 4 | | Jill | | sd7sdfhgsdmn |
--------------------------------------------
| 5 | | John | | 5sdfsd4df7s5 |
--------------------------------------------
.
.
.
--------------------------------------------
| n | | name-n | | ran-n |
--------------------------------------------
(id为1的用户有用户名Bob)
(id为2的用户有用户名Alice)
等
videoview
-------------------------
| id | userID | videoID |
-------------------------
| 1 | 5 | 3 |
-------------------------
| 2 | 2 | 3 |
-------------------------
| 3 | 1 | 3 |
-------------------------
| 4 | 2 | 1 |
-------------------------
| 5 | 3 | 2 |
-------------------------
.
.
.
-------------------------
| n | user-n | video-n |
-------------------------
(id为5的用户从上表中查看了ID为3的视频)
(id为2的用户从上表中查看了ID为3的视频)
(id为1的用户从上表中查看id为3的视频)
(id为2的用户从上表中查看id为1的视频)
等
我需要的是从所有视频中获取viewcount
所以在这种情况下,结果应该是
1视频1(视频ID为1次1次)
1为视频2(视频id为2次,1次观看)
3视频3(视频3的视频3次)
等
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 ();
}
有效的代码,但是很麻烦
的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 );
}
}
但这可以通过很多代码实现这个
我想要做的还是在单个SELECT语句中计算viewcount,比如
的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
}
插入与上述相同的单个SELECT语句
到目前为止我尝试了什么
// add another LEFT JOIN
SELECT... , vv.videoID AS vvVideoID FROM video ... LEFT JOIN videoview AS vv ON vv.videoID = v.id ...
但是只列出视频信息1 - 1次,视频信息2 - 1次,视频信息3 - 3次等等,为我提供了许多参赛作品
这不是我想要的
我也尝试在那里添加一个计数(*),但只是在那里抛出一个$ getVideo未定义
希望有人可以提供帮助
答案 0 :(得分:0)
你可以做到这一点的一种方法是通过子查询:
SELECT *, (SELECT COUNT(*) FROM videoview WHERE `videoID` = `video`.`id`) as `views` FROM video
这会将字段views
添加到video
表的输出中。
答案 1 :(得分:0)
为什么不使用agregating function GROUP BY
?例如:
SELECT video.id, userinfo.username, COUNT(*) as viewcount FROM video INNER JOIN userinfo ON video.userID = userinfo.id LEFT JOIN videoview ON video.id = videoview.videoID GROUP BY videoview.videoID;