简体   繁体   English

SQL将ORDER BY结果作为数组返回

[英]SQL return ORDER BY result as an array

Is it possible to return groups as an associative array? 是否可以将组作为关联数组返回? I'd like to know if a pure SQL solution is possible. 我想知道是否可以使用纯SQL解决方案。 Note that I release that I could be making things more complex unnecessarily but this is mainly to give me an idea of the power of SQL. 请注意,我发布我可能会使事情变得更加复杂,但这主要是为了让我了解SQL的强大功能。

My problem: I have a list of words in the database that should be sorted alphabetically and grouped into separate groups according to the first letter of the word. 我的问题:我在数据库中有一个单词列表,应该按字母顺序排序,并根据单词的第一个字母分组到不同的组中。

For example: 例如:

ape
broom
coconut
banana
apple

should be returned as 应该归还为

array(
'a' => 'ape', 'apple',
'b' => 'banana', 'broom',
'c' => 'coconut'
)

so I can easily created sorted lists by first letter (ie clicking "A" only shows words starting with a, "B" with b, etc. This should make it easier for me to load everything in one query and make the sorted list JavaScript based, ie without having to reload the page (or use AJAX). 所以我可以通过第一个字母轻松创建排序列表(即点击“A”只显示以a开头的单词,“B”表示带b,等等。这样可以让我更容易在一个查询中加载所有内容并生成排序列表JavaScript基于,即无需重新加载页面(或使用AJAX)。

Side notes: I'm using PostgreSQL but a solution for MySQL would be fine too so I can try to port it to PostgreSQL. 附注:我正在使用PostgreSQL,但MySQL的解决方案也不错,所以我可以尝试将其移植到PostgreSQL。 Scripting language is PHP. 脚本语言是PHP。

MySQL: MySQL的:

SELECT LEFT(word, 1) AS first_letter, 
  GROUP_CONCAT(word) AS word_list
FROM MyTable
GROUP BY LEFT(word, 1);

PostgreSQL 8.4: PostgreSQL 8.4:

SELECT SUBSTRING(word FOR 1) AS first_letter, 
  ARRAY_TO_STRING(ARRAY_AGG(word), ',') AS word_list
FROM MyTable
GROUP BY SUBSTRING(word FOR 1);

See http://mssql-to-postgresql.blogspot.com/2007/12/cool-groupconcat.html for more about emulating MySQL's GROUP_CONCAT() in PostgreSQL. 有关在PostgreSQL中模拟MySQL的GROUP_CONCAT()更多信息,请参阅http://mssql-to-postgresql.blogspot.com/2007/12/cool-groupconcat.html

See http://www.postgresonline.com/journal/index.php?/archives/126-PostgreSQL-8.4-Faster-array-building-with-array_agg.html for more on ARRAY_AGG() . 有关ARRAY_AGG()更多信息,请参阅http://www.postgresonline.com/journal/index.php?/archives/126-PostgreSQL-8.4-Faster-array-building-with-array_agg.html

Run 26 separate queries. 运行26个单独的查询。 Or run one query and separate the results alphabetically server side. 或者运行一个查询并按字母顺序将服务器端分开。

Are you familiar with the LIKE syntax? 你熟悉LIKE语法吗?

as in 如在

SELECT * FROM words WHERE col LIKE a% ORDER BY col

would give you the a's in order and so on. 会给你一个有序的等等。 Build the hash accordingly. 相应地构建哈希。

I'm no PostgreSQL expert (or user) but I believe in the later versions you can do something like this: 我不是PostgreSQL专家(或用户),但我相信在以后的版本中你可以这样做:

SELECT 
    ROW_NUMBER() OVER (PARTITION BY SUBSTRING(whatever from 1 for 1) ORDER BY whatever) AS __ROW,
    whatever
FROM yourtable;

This is ANSI SQL. 这是ANSI SQL。 I don't know what the support on MySql is like yet. 我不知道MySql上的支持是什么样的。 Oracle and SQL Server both work with this syntax and I heard it through Google that PostgreSQL 8.4 supports windowing functions. Oracle和SQL Server都使用这种语法,我通过Google听说PostgreSQL 8.4支持窗口函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM