我正在尝试从db获取一些数据。
username
email
来自联系表使用两个表tutor
和institute
到目前为止,这是我的代码:
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
答案 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)
无需从username
和tutors
表调用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