Posts: 282
Threads: 85
Joined: May 2015
Reputation:
0
Hello
I have 2 table that have relation both
I try to get result from table 2 with 2 way:
1: select table1.name,table2.score from table2 join table1 on table1.username = table2.username
2: select (select name from table1 where table1.username = table2.username) as name,score from table2
Now
Which way is good?
Note: i need name field only in table1
Posts: 135
Threads: 2
Joined: Jun 2016
Reputation:
3
I am not sure on this but i believe option is will be the faster one (if you have your index set correct)
The second one would create 2 selects and might not be able to use the index as efficiant.
But somebody with more DB knowledge might correct me on this.
On the package it said needs Windows 7 or better. So I installed Linux.
Posts: 830
Threads: 13
Joined: Jun 2015
Reputation:
50
I agree that the "join" version will optimize better and run faster.
Posts: 135
Threads: 2
Joined: Jun 2016
Reputation:
3
select table1.name,table2.score from table2 join table1 on table1.username = table2.username
it will only return table1.name,table2.score nothing else unless you add it to the field list
On the package it said needs Windows 7 or better. So I installed Linux.
Posts: 82
Threads: 12
Joined: May 2017
Reputation:
3
select table1.name,table2.score from table2 join table1 on table1.username = table2.username
This is equivalent and give you better picture
select t1.name as 'name', t2.score as 'score' from table2 t2
join table1 t1 on t1.username= t2.username
Run the query only show 2 columns : 'name' and 'score'
Regards
Posts: 920
Threads: 9
Joined: Jun 2016
Reputation:
54
09-29-2017, 04:00 PM
(This post was last modified: 09-29-2017, 04:15 PM by php_rocs.)
@omid_student,
Don't forget that you can use the EXPLAIN statement to determine which one is more optimized or can be improved.