0

我有 2 个选择语句。我想按字母顺序取名字并将其放在标题为 First 的列中,然后按字母顺序将其放在姓氏旁边的标题为 Last 的列中。

SELECT Name AS 'First'
FROM contactlist
ORDER BY Name ASC
LIMIT 1;

SELECT Name AS 'Last'
FROM contactlist
ORDER BY Name DESC
LIMIT 1;
4

1 回答 1

0

不知道你为什么需要这个,但你去:

SELECT First, Last
FROM (
  SELECT Name AS 'First'
  FROM contactlist
  ORDER BY Name ASC
  LIMIT 1
  ) t, (
  SELECT Name AS 'Last'
  FROM contactlist
  ORDER BY Name DESC
  LIMIT 1
  ) t2

取你所有的名字并按行号匹配它们可能会更有趣(ASC First to DESC Last)——像这样:

SELECT First, Last
FROM (
  SELECT Name AS 'First',
    @rowNum:=@rowNum + 1 rn
  FROM contactlist
    JOIN (SELECT @rowNum:= 0) r
  ORDER BY Name ASC
  ) t JOIN
 (
  SELECT Name AS 'Last',
    @rowNum2:=@rowNum2 + 1 rn
  FROM contactlist
    JOIN (SELECT @rowNum2:= 0) r
  ORDER BY Name DESC
  ) t2 ON t.rn = t2.rn

还有一些小提琴

于 2013-02-13T23:41:39.680 回答