这是我使用的查询工作正常,但我希望结果集按字母顺序排列在同一列中出现的第二个值。
SELECT userid
FROM my_table
WHERE value = $corrected_gradyear
and attributeid = 4
表格设置就像这样
attributeid userid value
3 1231 maidenname
2 1231 lastname
4 1231 1980
5 1231 address
8 1231 phone
9 1231 email
3 5 maidenname
2 5 lastname
4 5 1978
5 5 address
8 5 phone
9 5 email
我只想要用户ID ID,但我希望它们按属性3(婚前姓名)按字母顺序排序。
答案 0 :(得分:1)
这是你想要的吗?
select t1.userid,t1.value as year,t2.value as maidenname from my_table t1
join my_table t2 on t2.userid = t1.userid and t2.attributeid=3
where t1.attributeid=4
order by t1.value,t2.value
答案 1 :(得分:0)
这只是一张可怕的桌子。
我猜你要做的是:
SELECT VALUE as MaidenName
FROM my_table WHERE attributeID = 3
AND UserID IN
(SELECT userid
FROM my_table
WHERE value = $corrected_gradyear
and attributeid = 4)
ORDER BY MaidenName
答案 2 :(得分:0)
SELECT tUsers.userid
FROM my_table tUsers
INNER JOIN my_table tMaidenNames ON tMaidenNames.userid = tUsers.userid AND tMaidenNames.attributeid = 3
WHERE tUsers.value = $corrected_gradyear
AND tUsers.attributeid = 4
ORDER BY tMaidenNames.value ASC
答案 3 :(得分:0)
SELECT userid
, MAX(CASE WHEN attributeid = 4 THEN value END) corrected_grad_year
, MAX(CASE WHEN attributeid = 3 THEN value END) maidenname
FROM eav_hell
GROUP
BY userid
ORDER
BY whatever;