我在MySQL中有三个名为users
,user_academic_info
,department_info
的表,我想在这三个表上获得加入结果以获得类似搜索的选项。查询如下:
SELECT full_name, user_id, email, mobile, dept_name
FROM users, department_info
WHERE dept_id = (select dept_id from user_academic_info where ainfo_id = user_id)
AND full_name LIKE '%%'
OR user_id LIKE '%%'
OR email LIKE '%%'
OR mobile LIKE '%%'
OR dept_name LIKE '%%'
它向我展示了所有部门的所有用户,而在数据库中他们只属于一个部门。
答案 0 :(得分:0)
请参阅mysql join
SELECT * FROM `users` AS `u`
LEFT JOIN `user_academic_info` AS `ui` ON (
`u`.`id` = `ui`.`user_id`
)
LEFT JOIN `department_info` AS `di` ON (
`di`.`dept_id` =`ui`.`dept_id`
)
WHERE `u`.`full_name` LIKE '%%'
OR `u`.`id` = 4
以上查询应该可以获取所有3个表中的所有字段。 添加WHERE子句并在字段前添加正确的表名。