gpt4 book ai didi

mysql - 如何在mysql中合并多个select

转载 作者:太空宇宙 更新时间:2023-11-03 12:36:14 24 4
gpt4 key购买 nike

我有几个表,名称如下:

table_2012_12
table_2012_10
table_2012_07

结构相同:

id
order_id
account_id

我知道我必须执行这些查询:

SELECT * FROM table_2012_12 where id=1
SELECT * FROM table_2012_12 where id=5
SELECT * FROM table_2012_10 where id=1
SELECT * FROM table_2012_07 where id=22

使用单个查询的最佳(性能)方法是什么?

最佳答案

UNION 运算符就是这样做的。请参阅:http://dev.mysql.com/doc/refman/5.6/en/union.html

SELECT * FROM table_2012_12 where id=1
UNION ALL
SELECT * FROM table_2012_12 where id=5
UNION ALL
SELECT * FROM table_2012_10 where id=1
UNION ALL
SELECT * FROM table_2012_07 where id=22

通常,您不应该只写UNION,而应该写UNION ALLUNION 具有连接查询结果以及过滤掉任何重复行的效果。 UNION ALL 将简单地合并查询结果,并保留所有行。这样效率更高

关于mysql - 如何在mysql中合并多个select,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12996023/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com