2013-05-03 70 views
1

我想从数据库中获取一些数据。从3个表中选择数据

  1. username从接触表

登录表

  • email在两个表tutorinstitute

    与2个值检查这是到目前为止我的代码:

    SELECT s. * , c.email, l.username 
    FROM (
         SELECT contact_id AS id, 
            login_id, 
            username, 
            tutor_code AS code, 
            tutor_name AS Name, 
            'tutor' AS profile 
         FROM tutors 
         WHERE tutor_code = $code AND tutor_name = '$name' 
         UNION ALL 
         SELECT contact_id AS id, 
            login_id, 
            username, 
            institute_code AS code, 
            institute_name AS Name, 
            'institute' AS profile 
         FROM institutes 
         WHERE institute_code = $code AND institute_name = '$name' 
         ) 
    INNER JOIN contact c ON s.id = c.contact_id 
    INNER JOIN login l ON s.login_id = l.login_id 
    

    此查询不起作用,并且有错误消息。

    1054 - 在 '字段列表'

    UPDATE

    SELECT s. * , c.email, l.username 
    FROM (
         SELECT contact_id AS id, 
            login_id, 
            username, 
            tutor_code AS code, 
            tutor_name AS Name, 
            'tutor' AS profile 
         FROM tutors 
         WHERE tutor_code = $code AND tutor_name = '$name' 
         UNION ALL 
         SELECT contact_id AS id, 
            login_id, 
            username, 
            institute_code AS code, 
            institute_name AS Name, 
            'institute' AS profile 
         FROM institutes 
         WHERE institute_code = $code AND institute_name = '$name' 
         )s 
    INNER JOIN contact c ON s.id = c.contact_id 
    INNER JOIN login l ON s.login_id = l.login_id 
    
  • +0

    你确定用户名字段存在于登录表中吗? – ankurtr 2013-05-03 12:21:10

    +1

    你确定用户名在研究所和导师表中存在吗? – xQbert 2013-05-03 12:21:46

    +0

    http://stackoverflow.com/questions/16354226/make-a-select-query-from-two-table-with-where-clause ??? – nvanesch 2013-05-03 12:22:32

    回答

    1

    因为它似乎要检索您的usernamelogin,列username最有可能不tutors和/或institutes存在,也没有必要,因为你是在login_id加盟加盟login ,我想你可以只从子查询中删除username列:

    SELECT s. * , c.email, l.username 
    FROM (
         SELECT contact_id AS id, 
            login_id, 
            --username, 
            tutor_code AS code, 
            tutor_name AS Name, 
            'tutor' AS profile 
         FROM tutors 
         WHERE tutor_code = $code AND tutor_name = '$name' 
         UNION ALL 
         SELECT contact_id AS id, 
            login_id, 
            --username, 
            institute_code AS code, 
            institute_name AS Name, 
            'institute' AS profile 
         FROM institutes 
         WHERE institute_code = $code AND institute_name = '$name' 
         ) s 
    INNER JOIN contact c ON s.id = c.contact_id 
    INNER JOIN login l ON s.login_id = l.login_id 
    

    我还添加别名s您subuqery因为我以为我T的遗漏是一个错字,因为它会在没有抛出一个语法错误

    +0

    别名是type。你的解决方案是好的。它的工作现在。非常感谢你。 – TNK 2013-05-03 12:42:44

    +0

    非常欢迎。 – GarethD 2013-05-03 12:43:32

    -1

    这是在子查询中username场未发现未知列 '用户名',你必须在子查询中包含login表。

    SELECT s. * , c.email, l.username 
    FROM (
         SELECT contact_id AS id, 
            login_id, 
            l1.username, 
            tutor_code AS code, 
            tutor_name AS Name, 
            'tutor' AS profile 
         FROM tutors t 
         INNER JOIN login l1 ON l1.login_id = t.login_id 
         WHERE tutor_code = $code AND tutor_name = '$name' 
         UNION ALL 
         SELECT contact_id AS id, 
            login_id, 
            l2.username, 
            institute_code AS code, 
            institute_name AS Name, 
            'institute' AS profile 
         FROM institutes i 
         INNER JOIN login l2 ON l2.login_id = i.login_id 
         WHERE institute_code = $code AND institute_name = '$name' 
         ) 
    INNER JOIN contact c ON s.id = c.contact_id 
    INNER JOIN login l ON s.login_id = l.login_id 
    
    +0

    它不适合我.. – TNK 2013-05-03 12:29:36

    +0

    好的@TNK我忘记了'ON'关键字和'我'在研究所'INNER JOIN',我修改了答案,你可以再试一次吗? – 2013-05-03 12:43:49

    0

    没有必要从tutorsinstitutes表调用username和使用as abc当你的子查询关闭像

    SELECT s. * , c.email, l.username 
    FROM (
         SELECT contact_id AS id, 
            login_id, 
            tutor_code AS code, 
            tutor_name AS Name, 
            'tutor' AS profile 
         FROM tutors 
         WHERE tutor_code = $code AND tutor_name = '$name' 
         UNION ALL 
         SELECT contact_id AS id, 
            login_id, 
            institute_code AS code, 
            institute_name AS Name, 
            'institute' AS profile 
         FROM institutes 
         WHERE institute_code = $code AND institute_name = '$name' 
         ) as s 
    INNER JOIN contact c ON s.id = c.contact_id 
    INNER JOIN login l ON s.login_id = l.login_id 
    

    这个查询将返回重复条目,因为您在union中使用ALL

    希望它适合你。

    +1

    如果你将子查询别名为'abc',你还需要更新别名's'的使用位置('s。*','s.login_id = l.login_id','s.id = c.contact_id ') – GarethD 2013-05-03 12:39:16

    +0

    @GarethD是的你需要把's'更新为'abc',但我把'abc'更改为's' ..感谢你的建议.. – Rahul 2013-05-03 12:44:00