简体   繁体   English

具有2个内部联接的SQL计数

[英]SQL COUNT with 2 INNER JOINS

Video table stores id and video data. 视频表存储ID和视频数据。

Tag table stores id and tag_name. 标记表存储id和tag_name。

video_tag table connects video_ids and tag_ids to represent which video belongs to which tag. video_tag表连接video_id和tag_id,以表示哪个视频属于哪个标签。

For example in query below, i can get videos which belong to tags with ids both 3 and 4 例如,在下面的查询中,我可以获取属于ID 3和4的标签的视频

Also, i want to know how many rows are there. 另外,我想知道有多少行。 How should i modify the query? 我应该如何修改查询?

SELECT *
            FROM video
            INNER JOIN video_tag ON video.id = video_tag.video_id
            INNER JOIN tag ON tag.id = video_tag.tag_id
            WHERE video_tag.tag_id IN (3,4)
            GROUP BY video.id
            HAVING COUNT(video.id)=2
            ORDER BY video.id DESC 

* *

Table Structures: 表结构:

   --
    -- Table structure for table `video`
    --

    CREATE TABLE IF NOT EXISTS `video` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `original_id` varchar(20) COLLATE utf8_turkish_ci NOT NULL COMMENT 'alınan sitedeki id''si',
      `source` tinyint(2) NOT NULL,
      `title` varchar(160) COLLATE utf8_turkish_ci NOT NULL,
      `link` varchar(250) COLLATE utf8_turkish_ci NOT NULL,
      `image` varchar(300) COLLATE utf8_turkish_ci NOT NULL,
      `seconds` smallint(6) NOT NULL,
      `fullscreen` varchar(100) COLLATE utf8_turkish_ci NOT NULL,
      PRIMARY KEY (`id`),
      KEY `source` (`source`,`seconds`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_turkish_ci AUTO_INCREMENT=122987 ;

--
-- Table structure for table `tag`
--

CREATE TABLE IF NOT EXISTS `tag` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `tag_name` varchar(24) COLLATE utf8_turkish_ci NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `tag_name` (`tag_name`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_turkish_ci AUTO_INCREMENT=191 ;
--
-- Table structure for table `video_tag`
--

CREATE TABLE IF NOT EXISTS `video_tag` (
  `video_id` int(11) NOT NULL,
  `tag_id` int(11) NOT NULL,
  KEY `video_id` (`video_id`,`tag_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Your query should be doing what you want. 您的查询应该在做您想要的。 But, you can simplify it: 但是,您可以简化它:

SELECT v.*
FROM video v INNER JOIN
     video_tag vt
     ON v.id = vt.video_id
WHERE vt.tag_id IN (3, 4)
GROUP BY v.id
HAVING COUNT(v.id) = 2
ORDER BY v.id DESC ;

The only time this would not work is when a video can have duplicate tags of the same type. 只有当视频可以具有相同类型的重复标签时,这才行不通。 In that case, you case can instead use COUNT(DISTINCT) instead. 在这种情况下,您可以改用COUNT(DISTINCT)

If you want to return the query with the number of rows for, say, pagination, use SQL_CALC_FOUND_ROWS : 如果要返回带有分页的行数的查询,请使用SQL_CALC_FOUND_ROWS

SELECT SQL_CALC_FOUND_ROWS v.*
. . .

Then use FOUND_ROWS() . 然后使用FOUND_ROWS()

If you just want the number of rows, you can use a subquery, and further simplification: 如果只需要行数,则可以使用子查询,并进一步简化:

SELECT COUNT(*)
FROM (SELECT v.*
      FROM video_tag vt
      WHERE vt.tag_id IN (3, 4)
      GROUP BY vt.id
      HAVING COUNT(*) = 2
     ) t

Here is the complete query. 这是完整的查询。 It will show you every video, with it's total number. 它将向您显示每个视频及其总数。 Hope it helps. 希望能帮助到你。

SELECT v.id, COUNT(v.id) as [Number]
        FROM video AS v -- using alias
        INNER JOIN video_tag vt ON v.id = vt.video_id
        INNER JOIN tag t ON t.id = vt.tag_id
        WHERE vt.tag_id IN (3,4) -- this is optional; you can remove it
        GROUP BY v.id

Feel free to ask further. 欢迎进一步询问。

Total no of vedios in vedio table- 视频表中的vedios总数-

SELECT count(*) FROM video;

Total no. 总数 of rows those exist in other relational table also- 在其他关系表中存在的行中,

SELECT COUNT(*) 
            FROM video 
            INNER JOIN video_tag ON video.id = video_tag.video_id 
            INNER JOIN tag ON tag.id = video_tag.tag_id 

Total no of rows only for tag_id 3 and 4. 仅tag_id 3和4的行总数。

SELECT COUNT(*) 
            FROM video 
            INNER JOIN video_tag ON video.id = video_tag.video_id 
            INNER JOIN tag ON tag.id = video_tag.tag_id 
            WHERE video_tag.tag_id IN (3,4)

Total unique vedieo id for tag_id 3 and 4. tag_id 3和4的唯一标识总数。

SELECT COUNT(distinct video.id) 
            FROM video 
            INNER JOIN video_tag ON video.id = video_tag.video_id 
            INNER JOIN tag ON tag.id = video_tag.tag_id 
            WHERE video_tag.tag_id IN (3,4)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM