mysql - 在mysql联合查询中插入序列号
问题描述
我正在使用联合作为 mysql 查询
(Select id,c1,c2,c3 from table where c1=x1 and c2=x2 ) union <<---block 1
(Select id,c1,c2,c3 from table where c1=x1 and c2=x2 ) union <<---block 2
...
(Select id,c1,c2,c3 from table where c1=x1 and c2=x2 ) union <<---block mth
返回result
id|c1|c2|c3
------------
1 |..|..|..
2 |..|..|..
3 |..|..|..
.. .. .. ..
.. .. .. ..
nth.. .. ..
我想要添加一个 no 的表,显示结果是从哪个联合块获得的
id|c1|c2|c3|blk
---------------
1 |..|..|..|1
2 |..|..|..|1
3 |..|..|..|2
.. .. .. ..|3
.. .. .. ..|3
nth.. .. ..|3
解决方案
使用 blk 编号向每个查询添加 1 列:
(Select id,c1,c2,c3, 1 blk from table where c1=x1 and c2=x2 )
(Select id,c1,c2,c3, 2 from table where c1=x1 and c2=x2 )
...
(Select id,c1,c2,c3, <n> from table where c1=x1 and c2=x2 )
推荐阅读
- typescript - firebase 云消息不发送推送通知 - 参数“值”的值不是有效的查询约束
- prolog - 递归 | 列表嗖嗖序言中的自然数
- angular - 如何在Angular中将布尔标志从一个组件设置为另一个?
- javascript - 如何访问流中内容的预先计算的 MD5
- python-3.x - ModuleNotFoundError:没有名为“numpy.core.multiarray\r”的模块
- python - 将 .dat 文件转换为 .csv 文件的问题
- javascript - Superfish sf-js-enabled 似乎无法正常工作
- python-2.7 - 如何从字典中的字典中调用特定值?
- node.js - 如何确定 MongDB 中的 _id 字段是由 mongo 生成还是在插入期间由用户传递
- python - 将值拆分为单独的单元格