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