从3个表中选择数据

时间:2013-05-03 12:18:26

标签: mysql sql union subquery

我正在尝试从db获取一些数据。

    来自登录表的
  1. username
  2. email来自联系表
  3. 使用两个表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 - “字段列表”

    中的未知列'用户名'

    更新

    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
    

3 个答案:

答案 0 :(得分:1)

由于您似乎从username检索了login,因此username和/或tutors中很可能不存在institutes列,加入login也没有必要因为您加入login_id,我认为您可以从子查询中删除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

我还在你的subuqery中添加了别名s,因为我认为它的遗漏是一个错字,因为它会在缺席时抛出语法错误

答案 1 :(得分:0)

无需从usernametutors表调用institutes,并在子查询关闭时使用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

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

希望它适合你。

答案 2 :(得分:-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