我有多个表,每个表中都有Nr_Crt作为自动增量。 我能够显示查询结果但我的问题是这样的: 表1
nr_crt |item
1 |item 1
2 |item 2
3 |item 3
和表2
nr_crt |item
1 |song 1
2 |song 2
3 |song 3.
有没有办法像这样显示结果?
Nr. | Product
1 |item 1
2 |item 2
3 |item 3
4 |song 1
5 |song 2
6 |song 3
我知道我必须使用SELECT COUNT(*)FROM ..... 谢谢。
答案 0 :(得分:1)
这应该这样做:
SELECT @row := @row + 1 AS nr,
a.val AS product
FROM (SELECT item AS val
FROM table1
UNION ALL
SELECT item
FROM table2) a
JOIN (SELECT @row := 0) r
<强>结果强>
| NR | PRODUCT | ---------------- | 1 | item 1 | | 2 | item 2 | | 3 | item 3 | | 4 | song 1 | | 5 | song 2 | | 6 | song 3 |
答案 1 :(得分:0)
SELECT @rnk := @rnk + 1 nr_crt,
a.item
FROM
(
SELECT nr_crt, item, 1 ordr FROM TableA
UNION ALL
SELECT nr_crt, item, 2 ordr FROM TableA
) a, (SELECT @rnk := 0) b
ORDER BY a.ordr ASC, a.nr_crt ASC
输出
╔════════╦════════╗
║ NR_CRT ║ ITEM ║
╠════════╬════════╣
║ 1 ║ item 1 ║
║ 2 ║ item 2 ║
║ 3 ║ item 3 ║
║ 4 ║ item 1 ║
║ 5 ║ item 2 ║
║ 6 ║ item 3 ║
╚════════╩════════╝