0

我在 3 个(结构相同)表中有产品,包含 3 个列:id、date、requests。在搜索特定的产品 id 时,我想在所有表中搜索(我事先不知道可以在哪个表中找到产品 id)

table1 非常大(超过 1 亿行),目前我正在使用这个查询来创建一个临时表,我用它来执行其他查询。

CREATE TEMPORARY TABLE temp ENGINE=MEMORY 
as (select DISTINCT id, date, requests from table1 WHERE id='$id');

SELECT ... FROM temp

由于 table1 的大小(超过 1 亿行),使用 UNION 合并 3 个表然后查找 id 需要永远,所以这不是正确的方法。

我想出的最快方法是(我会使用 PHP 做到这一点):

search table1, if id found create temporary table, 
if id not found in table 1 then
search table2, if id found create temporary table, 
if id not found in table 2 then
search table3, if id found create temporary table, 
if id not found in table3 then return id not found. 

但是,这样我最终会执行多个查询(如果 id 位于表 3 中,则将执行 4 个查询)。

有没有更好的方法用 MySQL 做到这一点?

添加:

像这样的东西会起作用吗(什么是正确的语法,我下面的内容会引发错误)

CREATE TEMPORARY TABLE temp ENGINE=MEMORY 
as (
(select DISTINCT id, date, requests from table1 WHERE id='$id')
UNION
(select DISTINCT id, date, requests from table2 WHERE id='$id')
UNION
(select DISTINCT id, date, requests from table3 WHERE id='$id')
)
4

1 回答 1

0

尝试这个
SELECT * FROM information_schema.columns WHERE column_name = 'column_name';

或者

SELECT table_name, column_name FROM information_schema.columns WHERE column_name = 'column_name';

于 2017-01-26T08:40:02.113 回答