我有2个select语句我想组合成一个,但我真的只需要第二个select语句中的一个字段的信息(来自user_info_data的字段data
)。我需要的字段是名字,姓氏,电子邮件,课程全名,角色和字段data
,其中fieldid =' 15'。第一个select语句将为我提供除数据字段之外的所有内容。除了课程,第二个给了我一切。我尝试执行类似于Role字段的第二个select语句,但它抱怨它返回多行。如果我尝试使用课程名称而不使用fieldid =' 15'部分,它会带来超过10万条记录(每个用户都会显示每个课程及其所有数据)。
表格的字段:
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 john.doe@email.com是
所需的输出:
FirstName,LastName,电子邮件,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
答案 0 :(得分: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
。如果您有一天想要访问多个数据字段,则此重命名是一项规定。当你这样做时,你会多次包括这个表,每个表都需要一个表。另请参阅this answer有关如何处理此类数据格式的信息。