sql >> Database teknologi >  >> RDS >> Mysql

MySQL GROUP &COUNT Flere tabeller

Problem 1:

SELECT tag_id, tag_name, count(*)
FROM (
  SELECT tag_id, tag_name FROM tags
  UNION ALL
  SELECT tag_id, tag_name FROM more_tags
) s
GROUP BY tag_id, tag_name

Problem 2:

SELECT tag_id, tag_name, 'not present in more tags' as description
FROM tags LEFT JOIN more_tags ON tags.tag_id=more_tags.tag_id
WHERE more_tags.tag_id IS NULL
UNION ALL
SELECT tag_id, tag_name, 'not present in tags' as description
FROM tags RIGHT JOIN more_tags ON tags.tag_id=more_tags.tag_id
WHERE tags.tag_id IS NULL

Problem 3:

SELECT tag_id, tag_name, COUNT(*)
FROM tags INNER JOIN more_tags ON tags.tag_id=more_tags.tag_id
GROUP BY tag_id, tag_name


  1. Beregn decil ud fra frekvens i MySQL

  2. Forskellen mellem disse to sammenføjningstabeller?

  3. SQLite Vælg Distinct

  4. Vælg en post, bare hvis den før den har en lavere værdi