簡體   English   中英

計算第二個表中與第一個表中的ID相匹配的條目

[英]Count entries from second Table that match id from first Table

這是基本的,但我不知道。 我有兩個表(SeriesTable和OtherTable)。 SeriesTable具有給定系列的所有信息,包括其ID(名為“ seriesID”的列)。 另外,OtherTable有一個名為“ seriescolumn”的列,該列也具有給定序列的ID。

我需要進行查詢,以計算OtherTable的“ seriescolumn”中與SeriesTable中的seriesid列匹配的每個條目。 因此,例如,如果SeriesTable中的seriesid為5,我需要計算OtherTable中有多少條目在series列中的值為5。

下面是我當前的代碼,該代碼只是從第一個表中獲取信息,但是我不知道如何正確地從OtherTable中計數匹配的條目。

    <?
    $rs= mysql_query("SELECT seriesid FROM SeriesTable ORDER BY seriesid DESC");
    while ($row= mysql_fetch_array($rs)) { ?>

    content

    <? } ?>

聽起來您需要聯接和按語句分組。

SELECT  s.seriesid, Count(*) As NumberOfSeries
FROM    SeriesTable s Join
        OtherTable o On s.seriesid = o.seriescolumn
Group By s.seriesid
ORDER BY seriesid DESC

這應該返回每個序列號以及其重復次數的計數。

可能最簡單的方法是在一個大型SQL查詢中使用count語句。

您可以使用GROUP BY子句根據需要按序列號對結果進行分組,並提供以下內容:

SELECT seriesid, COUNT(*) FROM SeriesTable, OtherTable 
WHERE seriescolumn=seriesid GROUP BY seriesid
SELECT seriesid, COUNT(seriescolumn)
FROM SeriesTable, OtherTable 
WHERE OtherTable.seriescolumn = SeriesTable.seriesid
GROUP BY seriesid;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM