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

时间:2013-07-21 05:53:00

标签: 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)

1 个答案:

答案 0 :(得分:0)

试试这个并检查它是否有效

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

Check To see YOu demo