절의 알 수없는 열
간단한 쿼리가 있습니다.
SELECT u_name AS user_name FROM users WHERE user_name = "john";
나는 얻는다 Unknown Column 'user_name' in where clause. 'user_name'이후에도 성명서의 다른 부분에서 언급 할 수 없습니까 select 'u_name as user_name'?
SQL은 오른쪽에서 왼쪽으로 거꾸로 평가됩니다. 따라서 where 절이 구문 분석되고 select 절보다 먼저 평가됩니다. 이로 인해 u_name과 user_name의 별명이 아직 발생하지 않았습니다.
이건 어떤가요:
SELECT u_name AS user_name FROM users HAVING user_name = "john";
다음 MySQL 매뉴얼 페이지를 참조하십시오. http://dev.mysql.com/doc/refman/5.0/en/select.html
"select_expr은 AS alias_name을 사용하여 별명을 지정할 수 있습니다. 별명은 표현식의 열 이름으로 사용되며 GROUP BY, ORDER BY 또는 HAVING 절에서 사용될 수 있습니다."
(...)
WHERE 절이 실행될 때 열 값이 아직 결정되지 않았기 때문에 WHERE 절에서 열 별명을 참조하는 것은 허용되지 않습니다. B.5.4.4 절“열 별칭 문제”를 참조하십시오.
select u_name as user_name from users where u_name = "john";
다음과 같이 생각하십시오. where 절이 먼저 평가되어 어떤 행 (또는 결합 된 행)이 리턴되어야하는지 결정합니다. where 절이 실행되면 select 절이 실행됩니다.
더 나은 방법으로 넣으려면 다음을 상상하십시오.
select distinct(u_name) as user_name from users where u_name = "john";
전반이 없으면 전반을 참조 할 수 없습니다. 항상 먼저 평가를 받으면 select 절이 사용됩니다.
SELECT 문을 사용하여 데이터베이스에 실제로 존재하지 않는 새 필드를 만들고 다음과 같은 쿼리를 수행하려는 경우 (적어도 하나의 첨부 파일이있는 모든 노드 찾기) 해당 결과의 별칭은 동일한 문제가 발생합니다.
SELECT nodes.*, (SELECT (COUNT(*) FROM attachments
WHERE attachments.nodeid = nodes.id) AS attachmentcount
FROM nodes
WHERE attachmentcount > 0;
"WHERE 절에 알 수없는 '첨부 파일 개수'열"이라는 오류가 표시됩니다.
해결책은 실제로 매우 간단합니다. 별명을 생성하는 명령문으로 별명을 바꾸십시오.
SELECT nodes.*, (SELECT (COUNT(*) FROM attachments
WHERE attachments.nodeid = nodes.id) AS attachmentcount
FROM nodes
WHERE (SELECT (COUNT(*) FROM attachments WHERE attachments.nodeid = nodes.id) > 0;
여전히 별칭이 반환되지만 이제는 알 수없는 별칭으로 SQL이 중단되지 않아야합니다.
정의 된 내용 alias은이 WHERE절을 사용해야하는 HAVING절에서 환영하지 않습니다.
SELECT u_name AS user_name FROM users HAVING user_name = "john";
또는 원래 열 이름을 WHERE
SELECT u_name AS user_name FROM users WHERE u_name = "john";
하위 쿼리의 결과로 사용자 정의 별칭에 결과를 가져 오는 것과 동일 HAVING합니다.WHERE
SELECT u_name AS user_name ,
(SELECT last_name FROM users2 WHERE id=users.id) as user_last_name
FROM users WHERE u_name = "john" HAVING user_last_name ='smith'
수정 :
SELECT u_name AS user_name FROM users WHERE u_name = 'john';
어느 한 쪽:
SELECT u_name AS user_name
FROM users
WHERE u_name = "john";
또는:
SELECT user_name
from
(
SELECT u_name AS user_name
FROM users
)
WHERE u_name = "john";
RDBMS가 인라인 보기로의 술어 푸시를 지원하는 경우 후자는 전자와 동일해야합니다.
No you need to select it with correct name. If you gave the table you select from an alias you can use that though.
No you cannot. user_name is doesn't exist until return time.
Unknown column in WHERE clause caused by lines 1 and 2 and resolved by line 3:
$sql = "SELECT * FROM users WHERE username =".$userName;$sql = "SELECT * FROM users WHERE username =".$userName."";$sql = "SELECT * FROM users WHERE username ='".$userName."'";
May be it helps.
You can
SET @somevar := '';
SELECT @somevar AS user_name FROM users WHERE (@somevar := `u_name`) = "john";
It works.
BUT MAKE SURE WHAT YOU DO!
- Indexes are NOT USED here
- There will be scanned FULL TABLE - you hasn't specified the LIMIT 1 part
- So, - THIS QUERY WILL BE SLLLOOOOOOW on huge tables.
But, may be it helps in some cases
Just had this problem.
Make sure there is no space in the name of the entity in the database.
e.g. ' user_name' instead of 'user_name'
While you can alias your tables within your query (i.e., "SELECT u.username FROM users u;"), you have to use the actual names of the columns you're referencing. AS only impacts how the fields are returned.
Not as far as I know in MS-SQL 2000/5. I've fallen foul of this in the past.
SELECT user_name
FROM
(
SELECT name AS user_name
FROM users
) AS test
WHERE user_name = "john"
I had the same problem, I found this useful.
mysql_query("SELECT * FROM `users` WHERE `user_name`='$user'");
remember to put $user in ' ' single quotes.
try your task using IN condition or OR condition and also this query is working on spark-1.6.x
SELECT patient, patient_id FROM `patient` WHERE patient IN ('User4', 'User3');
or
SELECT patient, patient_id FROM `patient` WHERE patient = 'User1' OR patient = 'User2';
참고URL : https://stackoverflow.com/questions/153598/unknown-column-in-where-clause
'Programing' 카테고리의 다른 글
| 터미널에서 스위프트를 어떻게 사용합니까? (0) | 2020.08.04 |
|---|---|
| Android에서 Progressive Web Apps의 기능과 기본 앱의 기능 및 그 반대로의 기능 (0) | 2020.08.03 |
| 복제 방법을 올바르게 재정의하는 방법은 무엇입니까? (0) | 2020.08.03 |
| Makefile에서 여러 줄 문자열 변수를 만들 수 있습니까? (0) | 2020.08.03 |
| 그룹화 된 팬더 데이터 프레임을 반복하는 방법? (0) | 2020.08.03 |