1

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

  1. username从登录表
  2. email从联系表

检查两个表中的 2 个值tutorinstitute

到目前为止,这是我的代码:

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
4

3 回答 3

1

由于您似乎正在检索您的usernamefrom login,该列很可能在and/orusername中不存在,连接也不是必要的,因为您正在加入,我认为您可以从子查询中删除列:tutorsinstitutesloginlogin_idusername

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 中添加了别名,因为我认为它的遗漏是一个错字,因为它会在没有它的情况下引发语法错误

于 2013-05-03T12:34:35.837 回答
0

当您的子查询关闭时,无需从表中username调用并tutors使用institutesas 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

此查询将返回 Duplicate entries,因为您使用的是ALLin union

希望对你有效。

于 2013-05-03T12:37:18.560 回答
-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
于 2013-05-03T12:22:32.837 回答