我有一个表,其中所有userID都不同,因为我需要一个主键,但98个不同的UserID链接到一个用户名。我需要从名为studenttable
的数据库中选择一个用户,但仍然会调用userID
。
这是表格的一个较短的例子:
**UserID Username Year Form Subject1 Subject2 Subject3 Subject4**
1 John 13 D Art French Psychology Religion
2 John 13 D Art French Psychology Religion
.
.
99 Fank 13 D Maths Art Geography Biology
100 Fank 13 D Maths Art Geography Biology
.
.
198 Max 12 A Psychology Maths Physics German
199 Max 12 A Psychology Maths Physics German
.
.
296 Henry 13 D Psychology Economics French P.E
297 Henry 13 D Psychology Economics French P.E
依旧......
我想从数据库中选择符合特定条件的所有用户,例如,他们必须在 13年和表单D 并且执行心理学作为他们的主题之一。然后我只想显示一个具有相同用户名的用户。
我希望结果表看起来像:
UserID Username Year Form Subject
1 John 13 D Psychology
296 Henry 13 D Psychology
任何帮助将不胜感激, 感谢
答案 0 :(得分:3)
表中的问题是主题可以位于四列Subject1 Subject2 Subject3 Subject4
的任意列中,为此您可以使用IN
谓词,如下所示:
WHERE 'Psyhcology' IN(Subject1, Subject2, Subject3, Subject4)
这样您的完整查询就是:
SELECT
UserId,
UserName,
Year,
Form,
'Psyhcology' AS Subject
FROM tablename
WHERE Year = 13
AND Form = 'D'
AND userID IN(SELECT UserID
FROM tablname
WHERE 'Psychology' IN(Subject1, Subject2, Subject3, Subject4));
请注意:此查询会为Name
提供重复的行,因为您有多个具有不同ID的名称:
╔═════════╦═══════════╦═══════╦═══════╦════════════╗
║ USERID ║ USERNAME ║ YEAR ║ FORM ║ SUBJECT ║
╠═════════╬═══════════╬═══════╬═══════╬════════════╣
║ 1 ║ John ║ 13 ║ D ║ Psyhcology ║
║ 2 ║ John ║ 13 ║ D ║ Psyhcology ║
║ 296 ║ Henry ║ 13 ║ D ║ Psyhcology ║
║ 297 ║ Henry ║ 13 ║ D ║ Psyhcology ║
╚═════════╩═══════════╩═══════╩═══════╩════════════╝
但是如果你想获得唯一的行,你必须为每组名称选择一个id,例如MIN(USerID)
:
SELECT
MIN(UserId) AS UserId,
UserName,
Year,
Form,
'Psyhcology' AS Subject
FROM users
WHERE Year = 13
AND Form = 'D'
AND userID IN(SELECT UserID
FROM users
WHERE 'Psychology' IN(Subject1,
Subject2,
Subject3,
Subject4))
GROUP BY Username, Year, Form, Subject;
这会给你:
╔═════════╦═══════════╦═══════╦═══════╦════════════╗
║ USERID ║ USERNAME ║ YEAR ║ FORM ║ SUBJECT ║
╠═════════╬═══════════╬═══════╬═══════╬════════════╣
║ 296 ║ Henry ║ 13 ║ D ║ Psyhcology ║
║ 1 ║ John ║ 13 ║ D ║ Psyhcology ║
╚═════════╩═══════════╩═══════╩═══════╩════════════╝
但是,我宁愿normalize通过为subjects
创建新表来代替SQL Fiddle Demo:
<强> Subjects
强>:
SubjectId
PK,SubjectName
。然后你的桌子会是这样的:
UserId
,UserName
,Year
,Form
,SubjectId
外键引用主题表。这样可以更轻松地查询这两个表。例如,如果要搜索拍摄特定主题的用户,只需加入两个表:
SELECT
MIN(u.UserId) AS UserId,
u.Username,
u.Year,
u.Form,
s.Subjectname
FROM users AS u
INNER JOIN subjects AS s ON u.subjectId = s.subjectId
WHERE s.subjectname = 'Psyhcology'
AND u.Year = 13
AND u.Form = 'D'
GROUP BY u.Username, u.Year, u.Form, s.Subjectname;
这将为您提供与以前相同的结果:
╔═════════╦═══════════╦═══════╦═══════╦═════════════╗
║ USERID ║ USERNAME ║ YEAR ║ FORM ║ SUBJECTNAME ║
╠═════════╬═══════════╬═══════╬═══════╬═════════════╣
║ 296 ║ Henry ║ 13 ║ D ║ Psyhcology ║
║ 1 ║ John ║ 13 ║ D ║ Psyhcology ║
╚═════════╩═══════════╩═══════╩═══════╩═════════════╝
这是一个带有修改表结构的演示:
答案 1 :(得分:0)
下面的聚合器MIN
是任意的,它将为相同的记录选择最小的值。如果需要,请使用任何其他聚合器,例如MAX
:
SELECT MIN(UserId), Username, Year, Form, Subject1, Subject2, Subject3, Subject4
FROM studenttable
GROUP BY Username, Year, Form, Subject1, Subject2, Subject3, Subject4
答案 2 :(得分:0)
我想从数据库中选择适合某个特定的所有用户 标准,例如,他们必须在第13年和表格D和做 心理学作为他们的主题之一。然后我只想显示一个 具有相同用户名的用户。
所以你想在你的桌子上找到这些行:
SELECT DISTINCT(s.userId) FROM studenttable s
WHERE s.year = 13 AND s.form='D'
and (s.subject1='Psychology' OR s.subject2='Psychology'
OR s.subject3='Psychology' OR s.subject3='Psychology')
DISTINCT允许在SQL查询中用于从结果集中删除重复项。
答案 3 :(得分:0)
你可以这样做。
select distinct(UserID), Username, Year, Form, Subject from studenttable where Year = 13
and Form = 'D' and userID in(select UserID
from studenttable
where 'Psychology' in (Subject1,
Subject2,
Subject3,
Subject4));