Guest User

Untitled

a guest
May 26th, 2018
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. SELECT LEFT(u.name, 1) AS letter, count(*) AS count FROM
  2. (
  3. SELECT name, 'Customer' AS origin FROM customer
  4. UNION ALL
  5. SELECT companyname AS name, 'Company' AS origin FROM company
  6. ) AS u
  7. GROUP BY letter
  8. ORDER BY u.name
  9.  
  10. letter | count | type
  11. --------------------------
  12. A 1 Customers
  13. B 3 Both
  14. D 1 Companies
  15. <...>
  16.  
  17. /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
  18. /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
  19. /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
  20. /*!40101 SET NAMES utf8 */;
  21. /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
  22. /*!40103 SET TIME_ZONE='+00:00' */;
  23. /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
  24. /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
  25. /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
  26. /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
  27.  
  28. -- Table structure for table `company`
  29. DROP TABLE IF EXISTS `company`;
  30. /*!40101 SET @saved_cs_client = @@character_set_client */;
  31. /*!40101 SET character_set_client = utf8 */;
  32. CREATE TABLE `company` (
  33. `customerid` int(10) unsigned NOT NULL,
  34. `companyname` varchar(45) NOT NULL,
  35. PRIMARY KEY (`customerid`),
  36. CONSTRAINT `fk_company$customerid` FOREIGN KEY (`customerid`) REFERENCES `customer` (`id`) ON DELETE CASCADE
  37. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  38. /*!40101 SET character_set_client = @saved_cs_client */;
  39.  
  40. -- Dumping data for table `company`
  41. LOCK TABLES `company` WRITE;
  42. /*!40000 ALTER TABLE `company` DISABLE KEYS */;
  43. INSERT INTO `company` VALUES (2,'Jane Solutions LLC'),(4,'Disney Company'),(5,'Bad Company');
  44. /*!40000 ALTER TABLE `company` ENABLE KEYS */;
  45. UNLOCK TABLES;
  46.  
  47. -- Table structure for table `customer`
  48. DROP TABLE IF EXISTS `customer`;
  49. /*!40101 SET @saved_cs_client = @@character_set_client */;
  50. /*!40101 SET character_set_client = utf8 */;
  51. CREATE TABLE `customer` (
  52. `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  53. `name` varchar(45) NOT NULL,
  54. `pseudonym` varchar(25) DEFAULT NULL,
  55. PRIMARY KEY (`id`)
  56. ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
  57. /*!40101 SET character_set_client = @saved_cs_client */;
  58.  
  59. -- Dumping data for table `customer`
  60. LOCK TABLES `customer` WRITE;
  61. /*!40000 ALTER TABLE `customer` DISABLE KEYS */;
  62. INSERT INTO `customer` VALUES (1,'John Smith','John (NY)'),(2,'Jane Smith',NULL),(3,'John Smith','John (LA)'),(4,'Tom Smith',NULL),(5,'Alice Smith',NULL),(6,'Bob Smith',NULL),(7,'Benoit Smith',NULL);
  63. /*!40000 ALTER TABLE `customer` ENABLE KEYS */;
  64. UNLOCK TABLES;
  65. /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
  66.  
  67. /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
  68. /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
  69. /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
  70. /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
  71. /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
  72. /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
  73. /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
Add Comment
Please, Sign In to add comment