0

我有 2 个选择语句,我想合并为一个,尽管我真的只需要第二个选择语句中一个字段的信息(data来自 user_info_data 的字段)。我需要的字段是名字、姓氏、电子邮件、课程全名、角色和datafieldid = '15' 的字段。第一个 select 语句将给我除数据字段之外的所有内容。第二个给了我除了课程之外的一切。我尝试执行类似于 Role 字段的第二个 select 语句,但它抱怨它返回不止一行。如果我尝试只使用没有 fieldid='15' 部分的课程名称,它会产生超过 100k 条记录(每个用户显示在每门课程及其所有数据中)。

表格字段:

user(id,auth,confirmed,policyagreed,username,password,idnumber,firstname,lastname,email,phone etc..)
user_info_data(id,userid,fieldid,data)
role(id,name,shortname,description,sortorder)
role_assignments(id,roleid,contextid,userid...)
context(id,contextlevel,instanceid,path,depth)

第一个声明:

SELECT user.firstname AS Firstname, user.lastname AS Lastname, user.email AS Email, course.fullname AS Course, role.name AS Role
FROM user AS user, course AS course,role,role_assignments AS asg
INNER JOIN context AS context ON asg.contextid=context.id   
WHERE context.contextlevel = 50
AND role.id=asg.roleid
AND user.id=asg.userid
AND context.instanceid=course.id

第一条语句的输出:

名字姓氏电子邮件课程角色

John Doe john.doe@email.com 课程名称 学生

第二个声明:

SELECT user.firstname AS 'First Name', user.lastname AS 'Last Name', user.email AS 'Email', user_info_data.data AS 'IBCLC Certified'
FROM user, user_info_data
WHERE user.id = user_info_data.userid
AND fieldid = '15'

第二条语句的输出:

名字 姓氏 电子邮件 IBCLC 认证

约翰·多伊 john.doe@email.com 是

期望的输出:

名字、姓氏、电子邮件、IBCLC 认证、课程、角色

我试过的其他选择语句:带来 9,494 条记录,但是现在 fieldid 为 15 的字段数据是可能选择的列表,这可能是为什么?

SELECT user.firstname AS Firstname, user.lastname AS Lastname, user.email AS Email, userdata.data, course.fullname AS Course, role.name AS Role
FROM user AS user, course AS course, user_info_data AS userdata, role,role_assignments AS asg
INNER JOIN context AS context ON asg.contextid=context.id   
WHERE context.contextlevel = 50
AND userdata.fieldid = 15
AND role.id=asg.roleid
AND user.id=asg.userid
AND context.instanceid=course.id
4

1 回答 1

1

user_info_data在您的第一个请求中添加了这样的内容:

SELECT user.firstname AS Firstname,
       user.lastname AS Lastname,
       user.email AS Email,
       course.fullname AS Course,
       role.name AS Role,
       ibclcCert.data AS 'IBCLC Certified'
FROM user,
     course,
     role,
     role_assignments AS asg,
     context,
     user_info_data AS ibclcCert
WHERE context.contextlevel = 50
  AND role.id=asg.roleid
  AND user.id=asg.userid
  AND context.instanceid=course.id
  AND asg.contextid=context.id
  AND ibclcCert.userid = user.id
  AND ibclcCert.fieldid = '15'

在这种情况下,我将user_info_data表引用重命名为表示实际字段的内容。ibclcCert如果您有一天想要访问多个数据字段,则此重命名是一项规定。当您这样做时,您将多次包含该表,为您需要的每个字段提供一个。另请参阅有关如何处理此类数据格式的答案。

于 2012-07-18T15:08:53.147 回答