0

我有 5 个数据库,假设他们的名字是A B C D E

所有数据库具有相同的表/结构/字段

我想使用表从 5 个数据库中获取结果SMSOutgoing,该字段是uid

它看起来像这样:

SELECT * OR JOIN 5 database A B C D E F
FROM `table` SMSOutgoing
WHERE uid = 1

不是所有的数据库都有uid=1,需要显示哪个数据库有结果

我运行 SMS Gateway,每个电话/1 个号码使用 1 个数据库,这就是为什么有这么多不同的数据库。

我花了几个小时来解决它,但总是出错,我认为我遵循了错误的指南(在 1 个数据库中加入多个表)

我迷路了,请帮忙,谢谢

4

1 回答 1

1

听起来您想在 SMSOutgoing 表中列出包含 uid = 1 的数据库。如果是这样,您应该可以使用UNION

SELECT DISTINCT 'DatabaseA' WhichDb
FROM DatabaseA.SMSOutgoing 
WHERE uid = 1
UNION
SELECT DISTINCT 'DatabaseB' WhichDb
FROM DatabaseB.SMSOutgoing 
WHERE uid = 1
UNION
...
UNION
SELECT DISTINCT 'DatabaseF' WhichDb
FROM DatabaseF.SMSOutgoing 
WHERE uid = 1

I used DISTINCT in case you could have multiple uid in the same table -- that may be unnecessary.

EDIT: From your comments, it sounds like you just want the results:

SELECT *
FROM DatabaseA.SMSOutgoing 
WHERE uid = 1
UNION
SELECT *
FROM DatabaseB.SMSOutgoing 
WHERE uid = 1
UNION
...
UNION
SELECT *
FROM DatabaseF.SMSOutgoing 
WHERE uid = 1

You may need to use UNION ALL if you might have duplicates...

于 2013-03-29T18:42:22.137 回答