gpt4 book ai didi

mysql - 使用 UNION 从多个表中选择

转载 作者:行者123 更新时间:2023-11-30 23:55:18 26 4
gpt4 key购买 nike

我尝试使用 UNION 从一个数据库的 3 个表中选择记录,但出现错误:

#1248 - Every derived table must have its own alias

这是我正在尝试的查询:

SELECT * from
(SELECT column1, 'table1' from table1
UNION
SELECT column1, 'table2' from table2
UNION
SELECT column1, 'table3' from table3)
WHERE column1 not like 'abr%' and length(column1) < 8;

该错误的确切含义是什么?我该如何修复它并显示正确的结果?

最佳答案

SELECT Alis.column1 
FROM (SELECT column1, 'table1' as which from table1
UNION ALL
SELECT column1, 'table2' from table2
UNION ALL
SELECT column1, 'table3' from table3
) Alis
WHERE column1 not like 'abr%' and length(column1) < 8;

关于mysql - 使用 UNION 从多个表中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30555298/

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