gpt4 book ai didi

mysql - phpmyadmin where condition in mysql union 出现语法错误

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

我已经在 phpmyadmin (4.5.2) 中尝试过并且工作正常:

(SELECT 'db1' as DB_NAME, db1_col1, null as dbd2_col1 FROM db1.db1_table1)
UNION
(SELECT 'db2' as DB_NAME, null as dbd1_col1, db2_col1 FROM db2.db2_table1)

The result is:
DB_NAME db1_col1 dbd2_col1
db1 DB1_col1_val1 NULL
db2 NULL DB2_col1_val1

但是如果我尝试添加一个 where 子句,我会遇到语法错误:

(SELECT 'db1' as DB_NAME, db1_col1, null as dbd2_col1 
FROM db1.db1_table1
WHERE db1_col1 = 'DB1_col1_val1'
)
UNION
(SELECT 'db2' as DB_NAME, null as dbd1_col1, db2_col1
FROM db2.db2_table1
WHERE db2_col1 = 'DB1_col1_val1'
)

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNION (' at line 4

这让我发疯了!

然而,当我在控制台上尝试时,它按预期工作:

mysql> (SELECT 'db1' as DB_NAME, db1_col1, null as dbd2_col1
-> FROM db1.db1_table1
-> WHERE db1_col1 = 'DB1_col1_val1'
-> )
-> UNION
-> (SELECT 'db2' as DB_NAME, null as dbd1_col1, db2_col1
-> FROM db2.db2_table1
-> WHERE db2_col1 = 'DB1_col1_val1'
-> );
+---------+---------------+-----------+
| DB_NAME | db1_col1 | dbd2_col1 |
+---------+---------------+-----------+
| db1 | DB1_col1_val1 | NULL |
+---------+---------------+-----------+
1 row in set (0.00 sec)

还有问题:

这是 phpmyadmin 的错误还是我遗漏了什么?

最佳答案

因为它在控制台中工作,所以它可能是一个 phpmyadmin 错误。我的猜测是 phpmyadmin 试图在第一个查询的右括号后插入其 limit 子句。

更新

显然,这与已修复的错误相同 here .如果升级到 phpmyadmin v4.5.3.0 或更高版本,则此错误不再存在。

关于mysql - phpmyadmin where condition in mysql union 出现语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45897434/

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