我最初在此主题中发布了一个问题:
mysql - display the values that exists in ALL the tables
并且aleroot感谢建议了一个解决方案。我尝试过,一切似乎都有效。但是,我添加了更多表格,由于某种原因它不再有效。
作为测试,我在所有29个表中导入了相同的值。但是,当我运行下面的查询时,它不会返回任何行。不知道我做错了什么。任何帮助将不胜感激。感谢
SELECT @rownum:=0;
SELECT name FROM userinformation ui WHERE EXISTS (
SELECT user_id FROM userinformation t1
INNER JOIN table1 USING(user_id)
INNER JOIN table2 USING(user_id)
INNER JOIN table3 USING(user_id)
INNER JOIN table4 USING(user_id)
INNER JOIN table5 USING(user_id)
INNER JOIN table6 USING(user_id)
INNER JOIN table7 USING(user_id)
INNER JOIN table8 USING(user_id)
INNER JOIN table9 USING(user_id)
INNER JOIN table10 USING(user_id)
INNER JOIN table11 USING(user_id)
INNER JOIN table12 USING(user_id)
INNER JOIN table13 USING(user_id)
INNER JOIN table14 USING(user_id)
INNER JOIN table15 USING(user_id)
INNER JOIN table16 USING(user_id)
INNER JOIN table17 USING(user_id)
INNER JOIN table18 USING(user_id)
INNER JOIN table19 USING(user_id)
INNER JOIN table20 USING(user_id)
INNER JOIN table21 USING(user_id)
INNER JOIN table22 USING(user_id)
INNER JOIN table23 USING(user_id)
INNER JOIN table24 USING(user_id)
INNER JOIN table25 USING(user_id)
INNER JOIN table26 USING(user_id)
INNER JOIN table27 USING(user_id)
INNER JOIN table28 USING(user_id)
INNER JOIN table29 USING(user_id)
WHERE t1.user_id = ui.user_id
);
答案 0 :(得分:0)
我想,当你使用join时,系统必须获取所有记录,将它们连接在一起,然后应用过滤器(WHERE子句)。因此,当您尝试从29个表中获取数据时,实际上您正在尝试为每个user_id获取29 *(cols_num)字段,并且存在限制取决于您的数据库设置或实现。
尝试使用子查询或存在指令,例如
SELECT user_id FROM userinformation t1 WHERE
EXISTS (SELECT user_id FROM table_1 t WHERE t.user_id=t1.user_id) AND
EXISTS (SELECT user_id FROM table_2 t WHERE t.user_id=t1.user_id) AND
SO ON...