如何从多个表中选择所有数据?
如何从多个表中选择所有数据? 我尝试
`"SELECT * FROM `table1`, `table2`"`
, 但结果对我来说无法理解。它仅返回 table1
中的部分行,以及 table2
中所有数据的 3 倍。我在此处提出了一个相同的问题,但不明白答案。那你能帮我吗?提前致谢。
更新:
当我尝试时
(SELECT * FROM `table1`) UNION (SELECT * FROM `table2`)
它会返回 #1222 - 使用的 SELECT 语句具有不同的列数
how to select all the data from many tables?
i try
`"SELECT * FROM `table1`, `table2`"`
,
but result none understandable for me. it returns only some rows from table1
, and 3 times all the data from table2
. i've red one same question here, but don't understand the answer. so could you help me? thanks in advance.
update:
when i try
(SELECT * FROM `table1`) UNION (SELECT * FROM `table2`)
it returns
#1222 - The used SELECT statements have a different number of columns
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 UNION SELECT 构造
Use the UNION SELECT construct
您希望如何显示数据?两个表是否具有相同的架构?如果是这样,您可以使用 UNION 运算符。
http://www.w3schools.com/sql/sql_union.asp
How do you want the data displayed? Are both tables of the same schema? If so you could use the UNION operator.
http://www.w3schools.com/sql/sql_union.asp
如果您只是想显示许多表中的数据并且数据之间没有关系,则必须编写逻辑而不是数据库逻辑。
显示表(SQL 命令)
foreach 结果(您选择的编程语言)
select * from 表名(SQL 命令)
If you are just trying to show the data from many tables and there is no relationship between the data, you have to program logic instead of database logic.
show tables (SQL command)
foreach result (programming language of your choice)
select * from tablename (SQL command)
通过在两个表之间使用“,”进行选择并且没有 WHERE 子句,您正在执行隐式 两个表的交叉联接(两个表之间的所有行组合)。这可能不是您想要的。请参阅 UNION,如其他答案所述。
By doing that select with the "," between 2 tables and no WHERE clause, you are doing an implicit cross join of the 2 tables (all combinations of rows between the 2 tables). This is likely Not What You Want. See UNION, as mentioned by other answers.