mysql - 当给定的列字符串完全匹配时,如何将值增加到新表?

标签 mysql sql database distinct aggregate-functions

mySQL 可能很简单,但也可能很复杂,尤其是当第一次尝试从逻辑上理解如何执行任务并使表准确可见时。

我作业中的一个问题指出: 每家公司面试了几次?首先打印出接受采访次数最多的公司,然后按公司名称按 A-Z 顺序排序。

+------------------------------+------------+
| companyname                  | Interviews |
+------------------------------+------------+
| Ajax Software, Inc.          |          2 |
| Cameron Industries           |          2 |
| Flordia Software Designs     |          2 |
| Manhattan-Made Software      |          2 |
| Mountainside Magic Software  |          2 |
| Acme Information Source      |          1 |
| ApplDesign                   |          1 |
<<<<<<< cut out some output >>>>>>>>>>>>>>>>>>>
| Vegas Programming and Design |          1 |
| Virginia Software Industries |          1 |
+------------------------------+------------+
23 rows in set (0.00 sec)

我通过编码完美地得到了第一列:

SELECT DISTINCT companyname,
FROM interview
ORDER BY companyname ASC;

但是在面试中,我正在尝试获取代码,如果在公司名称中,有多少公司具有相同的字符串来增加到面试端。这是如何工作的?

完整输出:

+------------------------------+
| companyname                  |
+------------------------------+
| Acme Information Source      |
| Ajax Software, Inc.          |
| Ajax Software, Inc.          |
| ApplDesign                   |
| Bay Software Inc.            |
| Braddock Information Assoc.  |
| Buffalo Software Assoc.      |
| Cameron Industries           |
| Cameron Industries           |
| CCC Software                 |
| Davis-Klein Software         |
| DC Security Applications     |
| Flordia Software Designs     |
| Flordia Software Designs     |
| Focused Applications, Inc.   |
| Georgia Software Design      |
| Jersey Computer Services     |
| Long Island Apps, Inc.       |
| Manhattan-Made Software      |
| Manhattan-Made Software      |
| Mountainside Magic Software  |
| Mountainside Magic Software  |
| Nantucket Applications, Inc. |
| PennState Programming, Inc.  |
| Rochester Software Design    |
| Sandy Hook Software          |
| Vegas Programming and Design |
| Virginia Software Industries |
+------------------------------+
28 rows in set (0.00 sec)

最佳答案

尝试一下并检查它是否有效

SELECT companyname ,count(companyname) as interviews
from interview GROUP BY companyname order by interviews desc, companyname asc

Check To see YOu demo

关于mysql - 当给定的列字符串完全匹配时,如何将值增加到新表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17769511/

相关文章:

Mysqli 排名并列

c++ - qt:对 'mysql_something@nr' 的 undefined reference

mysql - 从 sql 备份文件更新 1 列

sql - PostgreSQL - GROUP BY 时间戳值?

php - Laravel Eloquent Relationships - 两个不同的送货地址与一个订单模型相关联

mysql - 如何在 MySQL 数据库中选择三种值类型中最新的一个?

php - 集成 php mysql 和 voip 以及文本到语音

sql - 在 PostgreSQL 表中查找循环引用?

php - 提高 PHP 文件中的 SQL 查询性能

sql - 如何在 SQL 中连接文件名的一部分?