Re:"I've isolated the bug to a database query"
by bhousel on Wednesday November 09, 2011 @11:02AM
(#3215990)
There's no reason ever to use a right join. I consider those to be a code smell.
[ Parent
| on HN ]
Re:"I've isolated the bug to a database query"
by og1 on Wednesday November 09, 2011 @02:22PM
(#3216970)
Agree with this as well. Forgot exactly what one was and googled it to make sure. The example that is used here would indicate orphaned records.
Re:"I've isolated the bug to a database query"
by ceejayoz on Wednesday November 09, 2011 @05:58PM
(#3217930)
There are valid use cases for orphaned records. A quote database might want to allow registered and anonymous submission, for example.
Re:"I've isolated the bug to a database query"
by derefr on Wednesday November 09, 2011 @10:26PM
(#3218729)
Or, ala Reddit, comments from deleted users.
Re:"I've isolated the bug to a database query"
by sesqu on Wednesday November 09, 2011 @11:48AM
(#3216212)
There's about as much reason to use a right join as there is to use a left join.
Re:"I've isolated the bug to a database query"
by bhousel on Wednesday November 09, 2011 @12:27PM
(#3216408)
Yes, but one of them means that you're thinking about the problem backwards.
Re:"I've isolated the bug to a database query"
by endersshadow on Wednesday November 09, 2011 @12:14PM
(#3216345)
I don't think I've ever used a right join in a production query (perhaps a couple of times when I was doing some digging and it was easier), but I don't understand your statement. Left joins are insanely useful. For example, getting all the sales for a salesperson:
select
sp.Name,
sum(s.Amount) as TotalSales
from
SalesPerson sp
left join Sales s on
sp.SalesPersonKey = s.SalesPersonKey
group by
sp.Name
order by
TotalSales desc
If you did a correlated subquery, it would take a ton of time to complete (`select sum(Amount) from Sales s where s.SalesPersonKey = sp.SalesPersonKey`), especially on large tables.Left joins (and full outer joins) are plenty useful and I use them almost daily. Care to explain what you mean?
Re:"I've isolated the bug to a database query"
by ckuehne on Wednesday November 09, 2011 @12:27PM
(#3216409)
What he (probably) means is that the following query returns the same result as your query:
select
sp.Name,
sum(s.Amount) as TotalSales
from
Sales s
RIGHT join SalesPerson sp on
sp.SalesPersonKey = s.SalesPersonKey
group by
sp.Name
order by
TotalSales desc
EDIT: Formatting.
Re:"I've isolated the bug to a database query"
by jtheory on Thursday November 10, 2011 @07:50AM
(#3219862)
That's what was meant, I assume, but it's misguided. Right joins don't create a "code smell" for technical reasons, but for practical reasons. Because left joins are the standard approach, if you're using a non-standard approach to a common problem you'd better have a damned good reason (because you're just confusing maintenance programmers... and thus causing errors... with no justification, otherwise).