MYSQL在where子句中的concat匹配

时间:2017-03-01 06:34:47

标签: mysql

我有2张桌子

课程 enter image description here

用户

enter image description here

我想展示课程作者=姓名+来自用户表的名字的课程

我试过

SELECT * FROM `courses` as c WHERE EXISTS ( SELECT * from `users` as u WHERE u.id_user = '63' AND c.author = CONCAT(u.name, ' ', u.surname)

我收到错误

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   靠近''在第1行

感谢。

3 个答案:

答案 0 :(得分:2)

你可以JOIN这两个表:

SELECT c.* FROM `courses` as c 
JOIN `users` as u 
ON c.author = CONCAT(u.name, ' ', u.surname)
WHERE u.id_user = '63' 

答案 1 :(得分:1)

connect system/a@xe as sysdba

create user pta identified by pta
/

grant dba to pta
/

grant create user to pta
/

grant alter user to pta
/

grant create role to pta
/

grant drop any role to pta
/

grant select on dba_role_privs to pta
/

grant select on role_tab_privs to pta
/

grant select on dba_roles to pta
/

你需要加入这里。

答案 2 :(得分:-1)

尝试

select * from courses where author in (select concat(name,' ',surname)  
from Users);