简体   繁体   English

mysql-从tableA,tableB选择*-问题

[英]mysql - select * from tableA, tableB - problem

is this a valid mysql statement ? 这是有效的mysql语句吗?

select * from TableA , TableB ;

if not what is the correct mysql statement for selecting the two tables. 如果不是,选择两个表的正确mysql语句是什么。

why i think there's something wrong with this is i wrote a mysql statement 为什么我认为这有什么问题是我写了一条mysql语句

select * from TableA, TableB INTO OUTFILE 'c:/test.csv' FIELDS TERMINATED BY ','  ENCLOSED BY '"'  LINES TERMINATED BY '\n' ;

and it went to endless loop . 然后陷入无休止的循环。 when i terminated. 当我终止时。 the csv file had grown into GBs in size. csv文件的大小已增长为GB。

It's not technically an endless loop - you are creating a cartesian product of the two tables -- that means that for each row in table a, every row in table b will be duplicated. 从技术上讲,这不是一个无限循环-您正在创建两个表的笛卡尔积-这意味着对于表a中的每一行,表b中的每一行都会重复。 This is almost never what you want to do -- it means that your output will be AXB rows, where A is the number of rows in table A and B is the number of rows in table B. 这几乎从来都不是您想要做的-这意味着您的输出将是AXB行,其中A是表A中的行数,B是表B中的行数。

You either want to accomplish this task by selecting two statements and appending to the file twice, UNIONing the two tables together, or JOINing them together. 您可以通过选择两个语句并两次附加到文件,将两个表联合在一起或将它们联接在一起来完成此任务。 Google for SQL Union, and INNER JOIN to learn more. Google for SQL Union和INNER JOIN了解更多。

That query is going to produce a cross join, so the number of rows will be the count of Table A * the count of Table B. 该查询将产生交叉联接,因此行数将是表A的数量*表B的数量。

If each of those tables had 100,000 rows in it, your CSV would end up with 10,000,000,000 rows, for example. 例如,如果每个表中都有100,000行,则CSV最终将以10,000,000,000行结尾。

If you only want to take all of the records of TableA and then all of the records from TableB in sequential order, the union keyword might be of interest. 如果只想获取TableA的所有记录,然后按顺序获取TableB的所有记录,则可能需要使用union关键字。

SELECT * FROM TableA
UNION
SELECT * FROM TableB 
    INTO OUTFILE 'c:/test.csv' 
    FIELDS TERMINATED BY ','  
    ENCLOSED BY '"'  
    LINES TERMINATED BY '\n' ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM