关于mysql中一条sql语句
发布网友
发布时间:2022-04-26 03:59
我来回答
共3个回答
热心网友
时间:2022-05-06 18:30
CREATE TABLE a (code varchar(8));
CREATE TABLE b (code varchar(8));
INSERT INTO a VALUES('A1');
INSERT INTO a VALUES('A2');
INSERT INTO a VALUES('A3');
INSERT INTO a VALUES('A4');
INSERT INTO a VALUES('A5');
INSERT INTO a VALUES('A6');
INSERT INTO a VALUES('A7');
INSERT INTO a VALUES('A8');
INSERT INTO a VALUES('A9');
INSERT INTO a VALUES('A0');
INSERT INTO b VALUES('B1');
INSERT INTO b VALUES('B2');
INSERT INTO b VALUES('B3');
INSERT INTO b VALUES('B4');
INSERT INTO b VALUES('B5');
INSERT INTO b VALUES('B6');
INSERT INTO b VALUES('B7');
INSERT INTO b VALUES('B8');
INSERT INTO b VALUES('B9');
INSERT INTO b VALUES('B0');
-- 错误的写法:
mysql> SELECT * FROM a LIMIT 5
-> UNION ALL
-> SELECT * FROM b LIMIT 10;
+------+
| code |
+------+
| A1 |
| A2 |
| A3 |
| A4 |
| A5 |
| B1 |
| B2 |
| B3 |
| B4 |
| B5 |
+------+
10 rows in set (0.00 sec)
-- 正确的写法:
mysql> (SELECT * FROM a LIMIT 5)
-> UNION ALL
-> (SELECT * FROM b LIMIT 10);
+------+
| code |
+------+
| A1 |
| A2 |
| A3 |
| A4 |
| A5 |
| B1 |
| B2 |
| B3 |
| B4 |
| B5 |
| B6 |
| B7 |
| B8 |
| B9 |
| B0 |
+------+
15 rows in set (0.00 sec)
热心网友
时间:2022-05-06 19:48
如果两个表结构一样可以用UNION连起来
SELECT * FROM table1 LIMIT 5
UNION ALL
SELECT * FROM table2 LIMIT 10
热心网友
时间:2022-05-06 21:23
这两个表关系呢?追问没什么关系,就是表结构一样
追答表结构一样 楼下正解