mysql 主键排序_MySQL按主键排序

bd96500e110b49cbb3cd949968f18be7.png

Some SQL servers allow for a generic statement such as ORDER BY PRIMARY KEY. I don't believe this works for MySQL, is there any such workaround that would allow for automated selects across multiple tables or does it require a lookup query to determine the primary key?

The workaround I have been working on involves calling a SHOW COLUMNS FROM before running the query. Is there a more efficient way of doing this? Can MySQL determine the primary key of a table during the select process?

Update: There is no official way of doing this in MySQL or SQL in general as Gordon pointed out. SAP has custom functionality for it. There are workarounds, such as working with SHOW COLUMNS FROM table or the information_schema as John pointed out.

解决方案

MySQL generally pulls data out by insertion order which would be by primary key, but that aside you technically can do the same thing if you pull out the primary key column name and put it in an order by

SELECT whatever FROM table

ORDER BY

( SELECT `COLUMN_NAME`

FROM `information_schema`.`COLUMNS`

WHERE (`TABLE_SCHEMA` = 'dbName')

AND (`TABLE_NAME` = 'tableName')

AND (`COLUMN_KEY` = 'PRI')

);

For composite keys you can use this

SELECT whatever FROM table

ORDER BY

( SELECT GROUP_CONCAT(`COLUMN_NAME` SEPARATOR ', ')

FROM `information_schema`.`COLUMNS`

WHERE (`TABLE_SCHEMA` = 'dbName')

AND (`TABLE_NAME` = 'tableName')

AND (`COLUMN_KEY` = 'PRI')

);

Permission for information schema access from the DOCS

Each MySQL user has the right to access these tables, but

can see only the rows in the tables that correspond to objects for

which the user has the proper access privileges. In some cases (for

example, the ROUTINE_DEFINITION column in the

INFORMATION_SCHEMA.ROUTINES table), users who have insufficient

privileges see NULL. These restrictions do not apply for InnoDB

tables; you can see them with only the PROCESS privilege.

The same privileges apply to selecting information from

INFORMATION_SCHEMA and viewing the same information through SHOW

statements. In either case, you must have some privilege on an object

to see information about it.

SETUP:

CREATE TABLE some_stuff (

firstID INT,

secondID INT,

username varchar(55),

PRIMARY KEY (firstID, secondID)

) ;

QUERY:

SELECT GROUP_CONCAT(`COLUMN_NAME` SEPARATOR ', ')

FROM `information_schema`.`COLUMNS`

WHERE (`TABLE_SCHEMA` = 'dbName')

AND (`TABLE_NAME` = 'some_stuff')

AND (`COLUMN_KEY` = 'PRI');

OUTPUT:

+--------------------------------------------+

| GROUP_CONCAT(`COLUMN_NAME` SEPARATOR ', ') |

+--------------------------------------------+

| firstID, secondID |

+--------------------------------------------+

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值