简体   繁体   中英

In MySQL Workbench, same output shown multiple times and by executing query, output rows increases by one

I have 2 tables:

Citizen_Location where it has Name and Location:

市民_位置

Citizens where it contains Name, Age & Exp:

公民

So, I've performed join operation in it:

select c.name,cl.location 
from citizens as c 
    join citizen_location as cl;

Output:

***564 row(s)*** returned   0.000 sec / 0.000 sec

Whenever I hit the execute button, that rows gets increased by 1 by default. Also, even executing normal query, output always shown twice and as your keep hitting execute button, it keeps increasing!

I tried to open new query tab and reinstalled the program but the problem remains the same. Everytime same output shown twice and thrice and so on.

I am getting output like this 输出看起来像

JOIN is short for INNER JOIN in SQL. It must be followed by a join condition which is either an ON clause or a USING clause. You have neither, so MySQL should raise a syntax error here. It can be considered a flaw in the DBMS that it doesn't.

Obviously, MySQL turns your JOIN into a CROSS JOIN instead, ie an inconditional join. Thus you join every row of the left table with every row of the right table.

The solution to your problem is simple: Just add a condition.

select c.name, cl.location 
from citizens as c 
join citizen_location as cl on cl.name = c.name;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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