开发者

Joins vs. other methods in mySQL

I'm completely self-taught, so there are obvious gaps in m开发者_如何学Cy knowledge. When I needed to be able to get data from more than one table, I learned how to do joins. For example, if I need a voucher number, account number, and balance from the vouchers table and a corresponding address from another table, I'd do this:

select v.voucherNbr, v.balanceInit, v.acctID, a.address, a.city from vouchers v
left join addresses a on v.acctID = a.id

which would return all the voucher records, and any addresses that happen to exist (in this case we want to return voucher records even if there's no corresponding address, hence the left join.)

I've recently inherited some code that appears to be trying to get the same data (and in this case, it does return the correct records) that does it like this:

select v.voucherNbr, v.balanceInit, v.acctID, a.address, a.city from vouchers v, addresses a 
where v.acctID = a.id

Can anyone explain to me the implications of doing that instead of using a join. As I said, in this particular case it does return the same data that the join does, but would it always?


It just happened to have the same results, but at some point it would not, because your query has an outer join, the syntax they provided you is an implicit inner join.

http://en.wikipedia.org/wiki/Join_(SQL)


It is a join, just an implicit one. This is a poor coding practice and should be replaced with an inner join.

The problem with this outdated code is that it is very easy to accidentally create a cross join and your code is harder to maintain (you should not mix implicit and explicit joins inthe same query or you might get wrong results).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜