0

我是关系数据库的新手......我想列出同一个数据库中许多表中的所有数据。

我有一个包含其他表名称的表。像这样:

mysql> select * from CarKeys;


+-------+
| Name  |
+-------+
| Audi  |
| Honda |
+-------+


mysql> select * from Audi;

+-------+---------+--------+
| Model | NumDoor | Colour |
+-------+---------+--------+
| A4    | 4       | White  |
+-------+---------+--------+


mysql> select * from Honda;
+------------+---------+--------+
| Model      | NumDoor | Colour |
+------------+---------+--------+
| Civic      | 4       | White  |
| Civic      | 4       | White  |
| HomdaModel | 5       | Red    |
+------------+---------+--------+

select * from Honda但现在,我不想单独打电话select * from Audi。我想要一个命令将select * from所有表命名为CarKeys

我试过这个:
SELECT * FROM (SELECT * FROM CarKeys);

但我收到一条错误消息:
ERROR 1248 (42000): Every derived table must have its own alias

如何获取此数据库中所有表的所有信息?我正在使用的模式的名称是WebApp

4

3 回答 3

1

this tables are haven't good relation, you can try:

SELECT * FROM Audi
UNION
SELECT * FROM Honda

Result:

+------------+---------+--------+
| Model      | NumDoor | Colour |
+------------+---------+--------+
| A4         | 4       | White  |
| Civic      | 4       | White  |
| Civic      | 4       | White  |
| HomdaModel | 5       | Red    |
+------------+---------+--------+
于 2013-04-15T10:00:57.793 回答
1

您的 SQL 设计存在大问题。你不应该这样称呼桌子。为什么不为所有汽车创建一个表并添加一个引用包含名称的表的列?

+-------+-------+
| ID    | Name  |
+-------+-------+
| 1     | Audi  |
| 2     | Honda |
+-------+-------+



+------------+---------+--------+--------+
| Model      | NumDoor | Colour | Brand  |
+------------+---------+--------+--------+
| Civic      | 4       | White  | 2      |
| A4         | 4       | White  | 1      |
| Civic      | 4       | White  | 2      |
| HomdaModel | 5       | Red    | 2      |
+------------+---------+--------+--------+
于 2013-04-15T09:50:32.033 回答
0

您必须使用存储过程

使用循环,将其过度Carkeys.Name存储在variable (v1)

现在使用Prepared Statement执行

select * from v1
于 2013-04-15T09:55:49.070 回答