mysql - 如何修复此错误并从多个表中进行选择?

标签 mysql sql database where-clause

我对列出表格有疑问,但我不明白我的错误。它说 列出与计划于 19951 年面试相关的公司名称、部门、雇主城市。

我正在尝试根据在采访表中找到的 qtrcode 列出记录。

我有这个代码:

SELECT companyname, division, city
FROM employer
WHERE interview.qtrcode = '%19951%';

ERROR 1054 (42S22): Unknown column 'interview.qtrcode' in 'where clause'

表格应该看起来像这样并且由于这个错误没有得到任何输出。

+------------------------------+------------------+------------------+
| companyname                  | division         | city             |
+------------------------------+------------------+------------------+
| Acme Information Source      | Customer Support | Cleveland        |
| Braddock Information Assoc.  | Payroll          | Boston           |
| Ajax Software, Inc.          | Production       | Berkeley         |
| Ajax Software, Inc.          | RandD            | Berkeley         |
| Bay Software Inc.            | Production       | Berkeley         |
| Vegas Programming and Design | Development      | Las Vegas        |
| Mountainside Magic Software  | Management       | Colorado Springs |
| Mountainside Magic Software  | Customer Support | Colorado Springs |
+------------------------------+------------------+------------------+
8 rows in set (0.00 sec)

面试表:

+----+------------+------------------------------+------------------+-------+---------+------+-------+----------+
| ID | Date       | Companyname                  | division         | Qtr.  | $ offer | Hrs. | List? | jobdesc  |
+----+------------+------------------------------+------------------+-------+---------+------+-------+----------+
|  1 | 1995-06-01 | Acme Information Source      | Customer Support | 19951 |   12.00 |   40 | y     | Job Desc |
|  2 | 1995-06-01 | Braddock Information Assoc.  | Payroll          | 19951 |   10.75 |   40 | n     | Job Desc |
|  3 | 1995-06-30 | Ajax Software, Inc.          | Production       | 19951 |   11.00 |   40 | y     | Job Desc |
|  4 | 1995-06-30 | Ajax Software, Inc.          | RandD            | 19951 |   11.50 |   40 | y     | Job Desc |
|  5 | 1995-07-01 | Bay Software Inc.            | Production       | 19951 |   11.75 |   20 | y     | Job Desc |
|  6 | 1995-08-01 | Vegas Programming and Design | Development      | 19951 |   11.00 |   40 | n     | Job Desc |
|  7 | 1995-08-01 | Mountainside Magic Software  | Management       | 19951 |   11.50 |   20 | y     | Job Desc |
|  8 | 1995-08-02 | Mountainside Magic Software  | Customer Support | 19951 |   10.75 |   40 | y     | Job Desc |
|  9 | 1995-12-01 | Cameron Industries           | Management       | 19953 |   13.25 |   35 | n     | Job Desc |
| 10 | 1995-12-02 | Cameron Industries           | Customer Support | 19953 |   13.00 |   35 | n     | Job Desc |
| 11 | 1995-12-04 | CCC Software                 | Production       | 19953 |   11.50 |   40 | n     | Job Desc |
| 12 | 1996-01-21 | DC Security Applications     | RandD            | 19953 |   11.50 |   30 | n     | Job Desc |
| 13 | 1996-02-01 | Davis-Klein Software         | RandD            | 19953 |   11.25 |   20 | n     | Job Desc |
| 14 | 1996-02-02 | Flordia Software Designs     | RandD            | 19953 |   11.75 |   30 | n     | Job Desc |
| 15 | 1996-07-01 | Rochester Software Design    | Development      | 19961 |   12.00 |   30 | n     | Job Desc |
| 16 | 1996-07-01 | Focused Applications, Inc.   | Production       | 19961 |   12.00 |   30 | y     | Job Desc |
| 17 | 1996-08-01 | Georgia Software Design      | RandD            | 19961 |   11.75 |   40 | y     | Job Desc |
| 18 | 1996-08-08 | ApplDesign                   | RandD            | 19961 |   11.50 |   30 | y     | Job Desc |
| 19 | 1996-08-11 | Long Island Apps, Inc.       | Development      | 19961 |   12.25 |   40 | y     | Job Desc |
| 20 | 1997-01-01 | Jersey Computer Services     | Customer Support | 19963 |   12.50 |   35 | n     | Job Desc |
| 21 | 1997-01-01 | Flordia Software Designs     | Development      | 19963 |   12.75 |   30 | n     | Job Desc |
| 22 | 1997-01-31 | Manhattan-Made Software      | Customer Support | 19963 |   12.50 |   40 | y     | Job Desc |
| 23 | 1997-02-01 | Manhattan-Made Software      | Payroll          | 19963 |   13.25 |   40 | y     | Job Desc |
| 24 | 1997-03-24 | Nantucket Applications, Inc. | RandD            | 19964 |   13.00 |   40 | y     | Job Desc |
| 25 | 1997-03-31 | PennState Programming, Inc.  | Management       | 19964 |   13.00 |   40 | y     | Job Desc |
| 26 | 1997-04-20 | Sandy Hook Software          | Development      | 19964 |   12.75 |   35 | n     | Job Desc |
| 27 | 1997-04-22 | Buffalo Software Assoc.      | Payroll          | 19964 |   12.75 |   35 | y     | Job Desc |
| 28 | 1997-05-01 | Virginia Software Industries | Production       | 19964 |   13.75 |   40 | n     | Job Desc |
+----+------------+------------------------------+------------------+-------+---------+------+-------+----------+

一组 28 行(0.00 秒)

雇主表:

|   companyname                    | division         | address                   | city             | statecode | zipcode |
| Acme Information Source        | Customer Support | 132 Commerical Way        | Cleveland        | OH        |   44234 |
| Ajax Software, Inc.            | RandD            | 2421 West Industrial Way  | Berkeley         | CA        |   94710 |

最佳答案

你有两个问题:

您的表名为 employer,您无法从面试中访问 qtrcode。

你要么加入面试表,要么错误只是在你应该有雇主的地方进行面试

如果是这种情况,您应该使用 LIKE 而不是 =:

SELECT companyname, division, city
FROM employer
WHERE employer.qtrcode LIKE '%19951%';

进行表面试时,您应该有这样的内容:

SELECT companyname, division, city
FROM employer, interview
WHERE employer.id = interview.employer_id 
      AND interview.qtrcode LIKE '%19951%';

编辑:

在 employer 中没有 id,为了得到你必须做的城市:

SELECT interview.companyname, interview.division, employer.city
FROM employer, interview
WHERE employer.Companyname = interview.Companyname                   
      AND interview.qtrcode LIKE '%19951%';

这行得通,但您确实应该在雇主表上有一个 ID,而不是在面试表上重复雇主数据。

您应该在 interview 表中有一个 employer_id 外键,如果您需要来自 employer 表的信息,您可以通过这个 id 获取它。

关于mysql - 如何修复此错误并从多个表中进行选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18092338/

相关文章:

php - 需要连接php和mysql

MySQL触发器不起作用

mysql - 不能对多列使用全文搜索

sql - 如何在SSIS中动态映射输入和输出列?

database - 在 Delphi 应用程序中使用哪种嵌入式数据库?

php - 为什么 SELECT 查询不能先出现,然后再进行 EDIT 查询

database - 帮助为 Twitter 网络应用程序设计数据库

php - 具有自定义索引 PHP 的 Foreach

php - jsTree 多树 MySQL/PHP 实现 - 嵌套集

c# - Linq-to-SQL:忽略 WHERE 子句中的空参数