即使插入了列,“WHERE”子句中的 SQL 未知列
Posted
技术标签:
【中文标题】即使插入了列,“WHERE”子句中的 SQL 未知列【英文标题】:SQL unknown column in 'WHERE' clause even though column is inserted 【发布时间】:2019-07-12 13:33:39 【问题描述】:我确定我使用的是单引号而不是双引号,但由于某种原因,它说性别列是未知的。
我尝试将“AND”语句更改为小写,尝试使用各种引号,还修复了在表内容和值中设置值的顺序。
CREATE DATABASE mysqlEXERCISE5;
USE MYSQLEXERCISE5;
CREATE TABLE myemployees (
gender VARCHAR(255),
first_name VARCHAR(255),
last_name VARCHAR(255),
birth_place VARCHAR(255),
citizen_ship VARCHAR(255),
yearly_wage INT(7) NOT NULL,
age INT(3) NOT NULL,
hours_worked INT(5) NOT NULL,
years_employed INT(6) NOT NULL
);
INSERT INTO myemployees (gender, first_name, last_name, birth_place, citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('M', 'Filip', 'Lazarevic', 'Montenegro', 'Australia', 30000, 19, 8000, 4);
INSERT INTO myemployees (gender, first_name, last_name, birth_place, citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('M', 'Daniel', 'Craig', 'USA', 'England', 75000, 37, 26000, 13);
INSERT INTO myemployees (gender, first_name, last_name, birth_place, citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('M', 'John', 'Jovovich', 'Serbia', 'USA', 55000, 27, 8000, 4);
INSERT INTO myemployees (gender, first_name, last_name, birth_place, citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('M', 'Stefan', 'Stojic', 'Croatia', 'Croatia', 'Spain', 100000, 24, 12000, 6);
INSERT INTO myemployees (gender, first_name, last_name, birth_place, citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('M', 'David', 'Longbottom', 'Ireland', 'UK', 67000, 43, 10000, 5);
INSERT INTO myemployees (gender, first_name, last_name, birth_place, citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('M', 'Riley', 'Reynolds', 'Canada', 'Canada', 90000, 30, 20000, 10);
INSERT INTO myemployees (gender, first_name, last_name, birth_place, citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('F', 'Riley', 'Raymond', 'Iceland', 'Greenland', 22000, 19, 6000, 3);
INSERT INTO myemployees (gender, first_name, last_name, birth_place, citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('M', 'Jade', 'Michaels', 'Egypt', 'France', 100000, 55, 30000, 15);
INSERT INTO myemployees (gender, first_name, last_name, birth_place, citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('F', 'Jade', 'Smith', 'Syria', 'India', 10000, 50, 10000, 5);
INSERT INTO myemployees (gender, first_name, last_name, birth_place, citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('M', 'Sam', 'Wilson', 'Scotland', 'Ireland', 34000, 25, 4000, 2);
INSERT INTO myemployees (gender, first_name, last_name, birth_place, citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('F', 'Sam', 'Wade', 'Russia', 'Finland', 55000, 32, 8000, 4);
SELECT * FROM myemployees WHERE first_name = 'Sam' AND gender = 'M';
SELECT * FROM myemployees WHERE first_name = 'Jade' AND gender = 'F';
我希望它显示性别为 M 的名为 Sam 的人以及性别为 F 的名为 Jade 的人
【问题讨论】:
你的 dbms 是什么? mysql还是mssql? VARCHAR(255) 用于性别列?? 另外,虽然名字是一种名字,出生地是一种地方,但公民身份不是一种船。 您必须提供与插入子句中指定的列相同数量的值。插入空值(如果您的表允许)或默认值以弥补所需的列数,或将要插入的列数更改为所需的值数。 【参考方案1】:首先从数据库中删除您的表,然后运行此查询
`CREATE TABLE myemployees (
gender VARCHAR(255),
first_name VARCHAR(255),
last_name VARCHAR(255),
birth_place VARCHAR(255),
citizen_ship VARCHAR(255),
yearly_wage INT NOT NULL,
age INT NOT NULL,
hours_worked INT NOT NULL,
years_employed INT NOT NULL
);
INSERT INTO myemployees (gender, first_name, last_name, birth_place,
citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('M', 'Filip', 'Lazarevic', 'Montenegro', 'Australia', 30000, 19,
8000,
4);
INSERT INTO myemployees (gender, first_name, last_name, birth_place,
citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('M', 'Daniel', 'Craig', 'USA', 'England', 75000, 37, 26000, 13);
INSERT INTO myemployees (gender, first_name, last_name, birth_place,
citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('M', 'John', 'Jovovich', 'Serbia', 'USA', 55000, 27, 8000, 4);
INSERT INTO myemployees (gender, first_name, last_name, birth_place,
citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('M', 'Stefan', 'Stojic', 'Croatia','Spain', 100000, 24, 12000, 6);
INSERT INTO myemployees (gender, first_name, last_name, birth_place,
citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('M', 'David', 'Longbottom', 'Ireland', 'UK', 67000, 43, 10000, 5);
INSERT INTO myemployees (gender, first_name, last_name, birth_place,
citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('M', 'Riley', 'Reynolds', 'Canada', 'Canada', 90000, 30, 20000, 10);
INSERT INTO myemployees (gender, first_name, last_name, birth_place,
citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('F', 'Riley', 'Raymond', 'Iceland', 'Greenland', 22000, 19, 6000, 3);
INSERT INTO myemployees (gender, first_name, last_name, birth_place,
citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('M', 'Jade', 'Michaels', 'Egypt', 'France', 100000, 55, 30000, 15);
INSERT INTO myemployees (gender, first_name, last_name, birth_place,
citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('F', 'Jade', 'Smith', 'Syria', 'India', 10000, 50, 10000, 5);
INSERT INTO myemployees (gender, first_name, last_name, birth_place,
citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('M', 'Sam', 'Wilson', 'Scotland', 'Ireland', 34000, 25, 4000, 2);
INSERT INTO myemployees (gender, first_name, last_name, birth_place,
citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('F', 'Sam', 'Wade', 'Russia', 'Finland', 55000, 32, 8000, 4);`
我希望它会帮助你。 See This Is Causing Error For You
【讨论】:
【参考方案2】:您在此列数据插入方面做错了,这就是您收到该错误的原因
INSERT INTO myemployees (gender, first_name, last_name, birth_place, citizen_ship,
yearly_wage, age, hours_worked, years_employed)
VALUES ('M', 'Stefan', 'Stojic', 'Croatia', 'Spain', 100000,24, 24, 6);
在这一行中,您提供了 10 个列值,但应该是 9 否则你的两个查询都可以正常工作
SELECT * FROM myemployees WHERE first_name = 'Sam' AND gender = 'M';
输出
gender first_name last_name birth_place citizen_ship yearly_wage age hours_worked years_employed
M Sam Wilson Scotland Ireland 34000 25 4000 2
SELECT * FROM myemployees WHERE first_name = 'Jade' AND gender = 'F';
输出
gender first_name last_name birth_place citizen_ship yearly_wage age hours_worked years_employed
F Jade Smith Syria India 10000 50 10000 5
here is the demo link
你可以使用union all
合并它们
SELECT * FROM myemployees WHERE first_name = 'Sam' AND gender = 'M'
union all
SELECT * FROM myemployees WHERE first_name = 'Jade' AND gender = 'F'
您可以使用显示@fa06 的in
而不是unoin all
,也可以使用显示@D-shish 的OR
【讨论】:
【参考方案3】:你可以试试IN
SELECT *
FROM myemployees
WHERE
first_name in ('Sam','Jade') AND gender in ('M' , 'F')
【讨论】:
【参考方案4】:你可以尝试使用or
架构 (MySQL v5.7)
CREATE TABLE myemployees (
gender VARCHAR(255),
first_name VARCHAR(255),
last_name VARCHAR(255),
birth_place VARCHAR(255),
citizen_ship VARCHAR(255),
yearly_wage INT(7) NOT NULL,
age INT(3) NOT NULL,
hours_worked INT(5) NOT NULL,
years_employed INT(6) NOT NULL
);
INSERT INTO myemployees (gender, first_name, last_name, birth_place, citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('M', 'Filip', 'Lazarevic', 'Montenegro', 'Australia', 30000, 19, 8000, 4);
INSERT INTO myemployees (gender, first_name, last_name, birth_place, citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('M', 'Daniel', 'Craig', 'USA', 'England', 75000, 37, 26000, 13);
INSERT INTO myemployees (gender, first_name, last_name, birth_place, citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('M', 'John', 'Jovovich', 'Serbia', 'USA', 55000, 27, 8000, 4);
INSERT INTO myemployees (gender, first_name, last_name, birth_place, citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('M', 'David', 'Longbottom', 'Ireland', 'UK', 67000, 43, 10000, 5);
INSERT INTO myemployees (gender, first_name, last_name, birth_place, citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('M', 'Riley', 'Reynolds', 'Canada', 'Canada', 90000, 30, 20000, 10);
INSERT INTO myemployees (gender, first_name, last_name, birth_place, citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('F', 'Riley', 'Raymond', 'Iceland', 'Greenland', 22000, 19, 6000, 3);
INSERT INTO myemployees (gender, first_name, last_name, birth_place, citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('M', 'Jade', 'Michaels', 'Egypt', 'France', 100000, 55, 30000, 15);
INSERT INTO myemployees (gender, first_name, last_name, birth_place, citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('F', 'Jade', 'Smith', 'Syria', 'India', 10000, 50, 10000, 5);
INSERT INTO myemployees (gender, first_name, last_name, birth_place, citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('M', 'Sam', 'Wilson', 'Scotland', 'Ireland', 34000, 25, 4000, 2);
INSERT INTO myemployees (gender, first_name, last_name, birth_place, citizen_ship, yearly_wage, age, hours_worked, years_employed)
VALUES ('F', 'Sam', 'Wade', 'Russia', 'Finland', 55000, 32, 8000, 4);
查询 #1
SELECT *
FROM myemployees
WHERE (first_name = 'Sam' AND gender = 'M') or (first_name = 'Jade' AND gender = 'F');
| gender | first_name | last_name | birth_place | citizen_ship | yearly_wage | age | hours_worked | years_employed |
| ------ | ---------- | --------- | ----------- | ------------ | ----------- | --- | ------------ | -------------- |
| F | Jade | Smith | Syria | India | 10000 | 50 | 10000 | 5 |
| M | Sam | Wilson | Scotland | Ireland | 34000 | 25 | 4000 | 2 |
View on DB Fiddle
【讨论】:
以上是关于即使插入了列,“WHERE”子句中的 SQL 未知列的主要内容,如果未能解决你的问题,请参考以下文章
SQLSTATE [42S22]:未找到列:1054 'where 子句'中的未知列 'id'(SQL:select * from `songs` where `id` = 5 limit 1)
不使用`where`子句,但得到错误:“未知列 在where子句中“
错误代码:1054 'where 子句'中的未知列'billId'