简体   繁体   中英

Difference between [table1.column1=table2.column1] and [table1.column1=1 AND table2.column1=1]

I don't know if there are any terms for these statements: I have table1 and table2

table1

id link_id
1     1
1     2
1     3

table2

id link_url
1     www.a
2     www.b
3     www.c

And two different MYSQL Statements:

SELECT table1.id as id, table2.link_url as link_url FROM table1, table2  WHERE table1.link_id =1 and table2.id=1

SELECT table1.id as id, table2.link_url as link_url FROM table1, table2  WHERE table1.link_id=table2.id

I understand that they both return the same results.

Is there any difference in using either of them or doesn't it matter at all?

Yes there is Difference. First Statement Returns only one row as you have set table1.link_id=1 and table2.id=1

Second Statement will Return every row which have link_id value in table1 similar to id value table2

link_id=table2.id select all data of your table where both coloumn are same .But WHERE table1.link_id =1 and table2.id=1 select data where link_id is 1 and id is 1.... both are diffrent

But USE JOIN is good option :-

SELECT table1.id as id, table2.link_url as link_url 
FROM table1 join  table2  
on table1.link_id=table2.id
WHERE table1.link_id =1 

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