简体   繁体   中英

SQL COUNT with 2 INNER JOINS

Video table stores id and video data.

Tag table stores id and tag_name.

video_tag table connects video_ids and tag_ids to represent which video belongs to which tag.

For example in query below, i can get videos which belong to tags with ids both 3 and 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.

If you want to return the query with the number of rows for, say, pagination, use SQL_CALC_FOUND_ROWS :

SELECT SQL_CALC_FOUND_ROWS v.*
. . .

Then use 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-

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.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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