Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
593 views
in Technique[技术] by (71.8m points)

Mysql constant field error occurred when doing related query [Err] 1064 - You have an error in your SQL syntax;

I use this sql to do query testing :

select '1101' as login_id,
a.project_id,a.user_id
from table1 as a on login_id = a.user_id

But the error message is:

[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'on login_id = a.user_id' at line 3

The data in table1 likes this:

id project_id user_id
1 A001 1002
2 A002 1001
3 A001 1001
4 A004 1003
5 A005 1004

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

on is for table joining, in this case I think you want to do something like this :

select '1101' as login_id,
a.project_id,a.user_id
from table1 as a 
where a.user_id = '1101'

for the expect output you can use case :

select '1001' as login_id,
case when user_id = '1001' then project_id else null end as project_id,
case when user_id = '1001' then user_id else null end as user_id
from table1

here is the db<>fiddle.

just saw the parameter part, just add a parameter which replace 1001 in the syntax and I think the result will be good. although I think keeping all those null data is a weird choose.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...