如何计算具有特定 ID 的连接表中的所有行
我有两张表,一张是歌曲列表。另一个是项目清单。
它们由歌曲ID 连接起来。我的查询当前如下所示:-
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
FROM $sTable
LEFT JOIN
$sTable2
ON ($sTable2.songs_id = $sTable.songsID)
$sWhere
$sOrder
$sLimit
";
$rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
$sTable 和 $sTable2 变量显然是两个表。
这工作正常并列出了“$sTable”中的所有行。您在上面看到的 JOIN 不需要列出歌曲,但这是我用我有限的 MySQL 能力所能得到的。
我想要做的是在 JSON 数据中返回另一列,显示“$sTable”中每首歌曲的所有项目(在 $sTable2 中)的总计数。因此计算每个具有特定“songsID”的项目。
$aColumns 如下:-
$aColumns = array( 'song_name', 'artist_band_name', 'author', 'song_artwork', 'song_file', 'genre', 'song_description', 'uploaded_time', 'emotion', 'tempo', 'songsID', 'user', 'happiness', 'instruments', 'similar_artists', 'play_count' );
这些是 $sTable 中的列,“songsID”是自动增量主键,它也存储在“$sTable2”中以将歌曲链接到其项目。
我需要能够将“projects_count”添加到上面的 $aColumns 数组中。
希望这次我能更好地解释自己。抱歉,我的 SQL 经验绝对很少。
I have two tables, one is a list of songs. The other is a list of projects.
They are joined by the songsID. My query currently looks like this:-
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
FROM $sTable
LEFT JOIN
$sTable2
ON ($sTable2.songs_id = $sTable.songsID)
$sWhere
$sOrder
$sLimit
";
$rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
The $sTable and $sTable2 variables are the two tables clearly.
This works fine and lists all the rows I have in '$sTable'. The JOIN that you see above is not necassary to list the songs but is as far as I am able to get with my limited MySQL ability.
What I would like to do is have another column returned in my JSON data that displays the total COUNT of all projects (in $sTable2) for EACH song in '$sTable'. Therefore counting each project which has a specific 'songsID'.
$aColumns is the following:-
$aColumns = array( 'song_name', 'artist_band_name', 'author', 'song_artwork', 'song_file', 'genre', 'song_description', 'uploaded_time', 'emotion', 'tempo', 'songsID', 'user', 'happiness', 'instruments', 'similar_artists', 'play_count' );
These are the columns in $sTable, with 'songsID' being the auto increment primary key which is also stored in '$sTable2' to link the songs to their projects.
I need to be able to add 'projects_count' into the $aColumns array above.
Hopefully I have explained myself better this time. Apologies that my SQL experience is absolutely minimal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将为您提供每个
songs_id
的项目数量。命令查询特定的 Song_id :
您还可以使用以下
This will give you the number of projects per
songs_id
.You can also query for specific song_id with:
And this: