【问题标题】:mysql with php joining and searching带有php加入和搜索的mysql
【发布时间】:2016-05-15 06:49:49
【问题描述】:

我在 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 '%%'  

它向我显示了所有部门的所有用户,就像在数据库中他们只属于一个部门一样。

【问题讨论】:

    标签: php mysql search join


    【解决方案1】:

    见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 子句并在字段前加上正确的表名。

    【讨论】:

    • 我得到了所有字段,但我想执行搜索操作,所以我已经使用了多次
    • 获得所有字段后,您可以像以前一样添加 WHERE 子句。只需在字段前添加表别名
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    相关资源
    最近更新 更多