如何计算具有特定 ID 的连接表中的所有行

发布于 2024-12-13 01:03:44 字数 1099 浏览 0 评论 0原文

我有两张表,一张是歌曲列表。另一个是项目清单。

它们由歌曲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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬シ愛 2024-12-20 01:03:45
select count(*) as projects_count, a.songs_id
from Table2 a
group by a.songs_id

这将为您提供每个 songs_id 的项目数量。

命令查询特定的 Song_id :

select count(*) as projects_count
from Table2 where Table2.songs_id = <your_song_id>

您还可以使用以下

select b.*, bb.projects_count from Table b 
left join (
   select count(*) as projects_count, a.songs_id
   from Table2 a
   group by a.songs_id
) bb on bb.songs_id = b.songsID
select count(*) as projects_count, a.songs_id
from Table2 a
group by a.songs_id

This will give you the number of projects per songs_id.

You can also query for specific song_id with:

select count(*) as projects_count
from Table2 where Table2.songs_id = <your_song_id>

And this:

select b.*, bb.projects_count from Table b 
left join (
   select count(*) as projects_count, a.songs_id
   from Table2 a
   group by a.songs_id
) bb on bb.songs_id = b.songsID
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文