ORDER BY mysql逗号分隔值

时间:2021-08-15 00:21:12

Hi i am trying to order my database by comma separated value but it doesn't seem to be working correctly for me. here is my mysql query.

嗨我想用逗号分隔值来命令我的数据库,但它似乎对我来说似乎没有正常工作。这是我的mysql查询。

   SELECT *
FROM wp_posts
LEFT JOIN wp_postmeta ON wp_posts.ID=wp_postmeta.post_id
LEFT JOIN wp_term_relationships ON wp_posts.ID=wp_term_relationships.object_id
LEFT JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id=wp_term_taxonomy.term_id
LEFT JOIN wp_terms ON wp_term_taxonomy.term_id=wp_terms.term_id
WHERE wp_terms.name = 'Dimmers'
AND meta_key = 'feature_number_of_channels'
GROUP BY wp_posts.ID
ORDER BY meta_value  ASC

And here is a screenshot. ORDER BY mysql逗号分隔值 As you can see from the screenshot they dont seem to be working correctly any help please on how to order these ASC correctly???

这是一个截图。从截图中可以看出,他们似乎没有正常工作任何帮助,请问如何正确订购这些ASC ???

Correct order i am looking for is so the highest out of them all then ASC like this

我正在寻找的正确的顺序是最高的,然后ASC就像这样

24,48 - 4,6,12,24 - 12 - 6 - 6 - 3

24,48 - 4,6,12,24 - 12 - 6 - 6 - 3

2 个解决方案

#1


3  

Change your ORDER BY to look at the integer value of the string :

更改ORDER BY以查看字符串的整数值:

ORDER BY CONVERT(SUBSTRING_INDEX(meta_value, ',', -1), SIGNED) DESC

Here's an example:

这是一个例子:

SELECT "24,38" AS meta_value UNION
SELECT 1 AS meta_value UNION
SELECT 30 AS meta_value
ORDER BY CONVERT(SUBSTRING_INDEX(meta_value, ',', -1), SIGNED) DESC

returns:

收益:

Rows = 3
meta_value
24,38
30
3

Good luck!

祝你好运!

#2


0  

Using order table.field ASC might help you as there are too many tables joined. That way the server would take the exact order from one single table.

使用order table.field ASC可能会帮助你,因为有太多的表连接。这样服务器就可以从一个表中获取确切的顺序。

#1


3  

Change your ORDER BY to look at the integer value of the string :

更改ORDER BY以查看字符串的整数值:

ORDER BY CONVERT(SUBSTRING_INDEX(meta_value, ',', -1), SIGNED) DESC

Here's an example:

这是一个例子:

SELECT "24,38" AS meta_value UNION
SELECT 1 AS meta_value UNION
SELECT 30 AS meta_value
ORDER BY CONVERT(SUBSTRING_INDEX(meta_value, ',', -1), SIGNED) DESC

returns:

收益:

Rows = 3
meta_value
24,38
30
3

Good luck!

祝你好运!

#2


0  

Using order table.field ASC might help you as there are too many tables joined. That way the server would take the exact order from one single table.

使用order table.field ASC可能会帮助你,因为有太多的表连接。这样服务器就可以从一个表中获取确切的顺序。