我正试图对EAV表进行操作,但我无法让它工作。 我得到了结果,但我无法对它进行排序。
这是我的疑问:
SELECT
formFields.uid,
formFields.formId,
formFields.postId,
formFields.name,
formFields.value
FROM formFields
INNER JOIN formFields aux
ON (aux.name = "date")
WHERE
formFields.formId = "1789"
AND formFields.deleted = 0
AND formFields.hidden = 0
ORDER BY aux.value DESC
这是表格:
uid formId postId name value
1 1789 1 title example title #1
2 1789 1 date 1341091600
3 1789 2 title example title #2
4 1789 2 date 1341092300
5 1789 3 title example title #3
6 1789 3 date 1341081200
这就是我需要的结果:
uid formId postId name value
3 1789 2 title example title #2
4 1789 2 date 1341092300
1 1789 1 title example title #1
2 1789 1 date 1341091600
5 1789 3 title example title #3
6 1789 3 date 1341081200
结果按名称列中具有日期的值排序 (这些值只是示例)
编辑:这是一个sqlfiddle:http://sqlfiddle.com/#!2/0d3c32/2
答案 0 :(得分:2)
即使这个问题也是一个极好的老问题。我想在这里保留关于如何在EAV上执行数据排序的准确答案。
为了达到原始问题的期望,可以使用以下SQL对指定列进行排序。
SELECT mdata.uid, mdata.formId, mdata.postId, mdata.name, mdata.value
FROM formFields mdata
WHERE mdata.formId = '1789'
AND mdata.deleted = 0
AND mdata.hidden = 0
ORDER BY (
SELECT s.value FROM formFields s
WHERE s.name='date'
AND s.postId=mdata.postId
) DESC
以上SQL将产生以下结果。
uid formId postId name value
3 1789 2 title example title #2
4 1789 2 date 1341092300
1 1789 1 title example title #1
2 1789 1 date 1341091600
5 1789 3 title example title #3
6 1789 3 date 1341081200
希望这个答案有所帮助。
答案 1 :(得分:1)
如果您想要ORDER BY
date
值,那么您可以使用以下内容:
SELECT
formFields.uid,
formFields.formId,
formFields.postId,
formFields.name,
formFields.value
FROM formFields
INNER JOIN formFields aux
ON (aux.name = "date")
WHERE formFields.formId = "1789"
AND formFields.deleted = 0
AND formFields.hidden = 0
ORDER BY case when formFields.name = 'date' then 0 else 1 end
答案 2 :(得分:0)
SELECT formFields.uid,
formFields.formId,
formFields.postId,
formFields.name,
formFields.value
FROM formFields tx_gcgluteusfeed_formFields
INNER JOIN formFields b
ON formFields.postId = b.postId
INNER JOIN formFields c
ON (c.name = "date")
WHERE ormFields.formId = "1789"
AND formFields.deleted = 0
AND formFields.hidden = 0
ORDER BY 1 DESC
如果您想要第一栏的订单