这样~一个字段 一行记录里面存一个数字select 字段 from table order by 字段 asc结果:
1
2
3
4
5
6
7现在我希望输出的结果第一行是字符串news结果像这样:
news
1
2
3
4
5
6`
`
`
`
`该怎么写
1
2
3
4
5
6
7现在我希望输出的结果第一行是字符串news结果像这样:
news
1
2
3
4
5
6`
`
`
`
`该怎么写
解决方案 »
- Toad for MySQL 6.0 Freeware debug存储过程就崩溃
- 如何将GBK的数据转成UTF8的?
- mysql导入数据的问题
- 出现Fatal error: Maximum execution time of 60 seconds exceeded in .....
- mysql无法启动?
- POSTGRESQL里如何用SQL访问其他POSTGRESQL数据库
- 请问如何在查询分析器中通过SQL语句把一个表中的字段存储到另一个表的字段里
- linux c mysql
- Mysql 中 mediumtext 类型 字段 为空怎么判断求大神
- mysql查找所有父节点
- my sql的安装问题
- 求求大家帮帮忙,mysql如何编写树形的结构的语句,谢谢,郁闷很长时间了
news
1
2
3
4
5
select `news` as 结果,0 as k
union all
select 字段,1 as k from table
) t
order by k,字段
union all
select 字段 from table order by 1
select 'news' as f1,0 as px from dual
union all
select 字段 ,1 from table ) a order by 1px
+------+
| col |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
+------+
7 rows in set (0.00 sec)mysql> select `结果` from(
-> select 'news' as `结果`,0 as k
-> union all
-> select col,1 as k from Lie_li
-> ) t
-> order by k,`结果`;
+------+
| 结果 |
+------+
| news |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
+------+
8 rows in set (0.05 sec)mysql>
我的那个字段是int的
select 'news' as `结果`,0 as k
union all
select col,1 as k from Lie_li
) t
order by k,`结果`;mysql> desc Lie_li;
+-------+---------+------+----
| Field | Type | Null | Key
+-------+---------+------+----
| col | int(11) | YES |
+-------+---------+------+----
1 row in set (0.02 sec)mysql>
select `结果` from(
select 'news' as `结果`,0 as k
union all
select col,1 as k from Lie_li
) t
order by k,`结果`;
mysql> select * from test;
+------+
| f1 |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+------+
5 rows in set (0.00 sec)mysql> select f1 from(
-> select 'news' as f1,0 as k
-> union all
-> select f1,1 as k from test) t
-> order by k,f1;
+------+
| f1 |
+------+
| news |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+------+
6 rows in set (0.00 sec)
UNION ALL
SELECT *,2 FROM LSB2
ORDER BY 2,CAST(F1 AS DECIMAL)
狼头 你那 结果 是什么啊 我看你的不是用单引号圈起来的啊
SELECT 'NEWS' AS F1,1 AS PX
UNION ALL
SELECT *,2 FROM LSB2
ORDER BY RIGHT(CONCAT('000000000000','NEWS'),5)
假设你最大只有5位
SELECT 'NEWS' AS F1,1 AS PX
UNION ALL
SELECT *,2 FROM LSB2 ) a
ORDER BY px,CAST(F1 AS DECIMAL)orSELECT f1 FROM (
SELECT 'NEWS' AS F1,1 AS PX
UNION ALL
SELECT *,2 FROM LSB2 ) a
ORDER BY px,RIGHT(CONCAT('000000000000',f1),5)
假设你最大只有5位假设你的数字>10的话,比如11-1000,那么加上RIGHT(CONCAT('000000000000',f1),5) 如何你的表中数据已经是有序的,
SELECT f1 FROM (
SELECT 'NEWS' AS F1
UNION ALL
SELECT * FROM LSB2 ) aSELECT 'NEWS' AS F1
UNION ALL
SELECT * FROM LSB2 ) a
SELECT f1 FROM (
SELECT 'NEWS' AS F1
UNION ALL
SELECT * FROM LSB2 ) a
狼头.......复制你的 然后把该改的改改 运行结果 news打头 数字乱的唉~
+------+
| col |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
| 16 |
| 17 |
| 18 |
| 19 |
| 20 |
| 21 |
| 22 |
| 23 |
| 24 |
| 25 |
| 26 |
+------+
26 rows in set (0.00 sec)mysql> select `结果` from(
-> select 'news' as `结果`,0 as k
-> union all
-> select col,1 as k from Lie_li
-> ) t
-> order by k,`结果`+0;
+------+
| 结果 |
+------+
| news |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
| 16 |
| 17 |
| 18 |
| 19 |
| 20 |
| 21 |
| 22 |
| 23 |
| 24 |
| 25 |
| 26 |
+------+
27 rows in set (0.00 sec)mysql>
union all
select col from Lie_li
order by `结果`+0
我这要求查询出来的结果 在字符上是不是有什么变动了? 能在SQL上硬性声明更改吗?
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
mysql_query("set names 'utf8'");
(
编号 int identity(1,1), --编号
结果 varchar(10) --你要存储的排序值
)
insert into @表变量(结果)values('news')
insert into @表变量(结果)select Convert(varchar(10),字段) from table order by 字段 asc
select 结果 from @表变量go试试这个吧,如果行请给分哦,楼主
搞定~~~
明天来结贴 呵呵